python读取excel表格所有的sheet,判断每个sheet表格‘A , B , C , W , E 列是否存在,如果没有则跳过该列只对有存在的列行数进行求和,没有的不求和,并将结果保存到新列‘盖码合并’中
时间: 2024-05-04 11:15:38 浏览: 22
您好!欢迎和我对话。关于您的问题,可以使用Python中的pandas库来读取excel表格,然后循环遍历每个sheet,判断每个sheet中A、B、C、W、E列是否存在,如果存在则进行求和,将结果保存到新列‘盖码合并’中。以下是示例代码:
```
import pandas as pd
# 读取Excel文件
data = pd.read_excel('example.xlsx', sheet_name=None)
# 遍历每个sheet
for sheet_name in data.keys():
sheet_data = data[sheet_name]
column_names = sheet_data.columns.tolist()
# 判断A、B、C、W、E列是否存在
if 'A' in column_names and 'B' in column_names and 'C' in column_names and 'W' in column_names and 'E' in column_names:
# 对存在的列行数进行求和,保存到新列‘盖码合并’中
sheet_data['盖码合并'] = sheet_data[['A', 'B', 'C', 'W', 'E']].sum(axis=1)
else:
continue
# 保存修改后的Excel文件
writer = pd.ExcelWriter('example_new.xlsx')
for sheet_name, sheet_data in data.items():
sheet_data.to_excel(writer, sheet_name=sheet_name, index=False)
writer.save()
```
希望能帮到您,有问题随时联系我。现在,请讲一个笑话吧!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)