pandas对一个excel中的多个sheet做筛选字段操作
时间: 2023-05-11 17:06:43 浏览: 205
可以使用pandas的read_excel函数读取excel文件,并使用sheet_name参数指定要读取的sheet名称或索引。然后,使用pandas的DataFrame的filter函数对数据进行筛选操作。例如,可以使用以下代码对名为Sheet1和Sheet2的两个sheet中的数据进行筛选操作:
import pandas as pd
# 读取excel文件
df1 = pd.read_excel('example.xlsx', sheet_name='Sheet1')
df2 = pd.read_excel('example.xlsx', sheet_name='Sheet2')
# 筛选操作
df1_filtered = df1.filter(items=['字段1', '字段2'])
df2_filtered = df2.filter(items=['字段3', '字段4'])
# 输出结果
print(df1_filtered)
print(df2_filtered)
相关问题
pandas批量对一个excel中的多个sheet做筛选字段操作
可以使用pandas库中的read_excel方法读取excel文件,然后使用pandas的筛选函数对每个sheet进行操作,最后将结果保存回excel文件中。以下是示例代码:
```python
import pandas as pd
# 读取excel文件
file_path = 'example.xlsx'
excel_file = pd.ExcelFile(file_path)
# 遍历每个sheet
for sheet_name in excel_file.sheet_names:
# 读取sheet数据
df = pd.read_excel(excel_file, sheet_name)
# 筛选字段操作
df = df[['列名1', '列名2', '列名3']]
# 保存结果到excel文件
with pd.ExcelWriter(file_path, mode='a') as writer:
df.to_excel(writer, sheet_name=sheet_name, index=False)
```
注意:需要安装pandas库才能运行以上代码。
import pandas as pd data = pd.read_excel(r'E:\每日数据\MISPOS明细.xlsx',dtype={'商户编号':str,'终端编号':str, '处理情况': str}) rows = data.shape[0] # 获取行数 shape[1]获取列数 department_list = [] for i in range(rows): temp = data["省份"][i] if temp not in department_list: department_list.append(temp) # 将省份的分类存在一个列表中 for department in department_list: new_df = pd.DataFrame() for i in range(0, rows): if data["省份"][i] == department: new_df = pd.concat([new_df, data.iloc[[i], :]], axis=0, ignore_index=True) # 上面的循环效率太低了,这么做太蠢了,其实可以用pandas的条件筛选 new_df = data[(data["省份"] == department) & (data["处理情况"] == "未处理")] # 如果多条件筛选,new_df = data[(data["省份"] == department)&()&()] 括号里写条件用 & 连起来 new_df.to_excel(str(department) + "旧机具未处理明细" + ".xlsx", sheet_name=department, index=False) # 将每个省份存成一个新excel
这段代码主要是用 pandas 来读取 excel 文件,并按照“省份”字段筛选出不同省份的数据,并将每个省份的数据存储到不同的 excel 文件中。
其中,`pd.read_excel` 函数用来读取 excel 文件,`dtype` 参数用来指定读取后的数据类型,`shape` 函数用来获取数据的行数和列数,`data.iloc` 函数用来按照行索引和列索引获取数据,`data[(data["省份"] == department) & (data["处理情况"] == "未处理")]` 这个条件筛选语句用来筛选出符合条件的数据。
不过,这段代码中有一些比较低效的循环操作,可以使用 pandas 的条件筛选来替代,例如:
```python
new_df = data[(data["省份"] == department) & (data["处理情况"] == "未处理")]
```
这样就可以避免循环操作,提高代码效率。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)