python读取excel表格所有的sheet,判断每个sheet表格‘A , B , C , W , E 列是否存在,如果没有则跳过该列只对有存在的列行数进行求和,没有的不求和,并将结果保存到新列‘盖码合并’中
时间: 2024-06-10 22:08:45 浏览: 11
你好,这个问题属于技术领域,我可以为你提供相关的解决方案。
你可以使用 Python 中的 pandas 和 openpyxl 库来读取 excel 表格并进行操作。具体步骤如下:
1. 使用 pandas 库的 read_excel 函数读取 excel 表格,得到一个字典,其中 key 是 sheet 名,value 是该 sheet 对应的 DataFrame。
2. 对于每个 DataFrame,使用 openpyxl 库的 load_workbook 函数读取该 sheet 对应的工作簿。
3. 使用 sheet 对象的 columns 属性获取该 sheet 的所有列。然后遍历这些列,判断该列是否存在 A,B,C,W,E 列,如果不存在则跳过,否则对该列进行求和。
4. 将结果保存到新列 ‘盖码合并’ 中。
5. 最后使用 pandas 库的 to_excel 函数将新的数据保存到 excel 表格中。
这样就可以实现你的要求了,希望对你有所帮助。
相关问题
python读取excel表格所有的sheet,使用 sheet 对象的 columns 属性获取该 sheet 的所有列。然后遍历这些列,判断该列是否存在 A,B,C,W,E 列,如果不存在则跳过,否则对该列进行求和。
可以使用 Pandas 库来读取 excel 表格。首先,用 Pandas 的 read_excel 函数读取 excel 表格,并返回一个字典,其中键是 sheet 名称,值是对应的 DataFrame。然后,对于每个 sheet 的 DataFrame,使用 columns 属性获取列名列表,然后判断 A,B,C,W,E 是否在该列表中。如果不存在,则跳过,否则对该列使用 sum 函数求和。
具体代码如下:
``` python
import pandas as pd
# 读取 excel 表格
xlsx = pd.read_excel('filename.xlsx', sheet_name=None)
# 遍历每个 sheet
for sheet_name, df in xlsx.items():
# 获取列名列表
columns = df.columns.tolist()
# 判断 A,B,C,W,E 是否在该列表中
if not set(['A', 'B', 'C', 'W', 'E']).issubset(set(columns)):
continue
# 对该列求和
sum_columns = df[['A', 'B', 'C', 'W', 'E']].sum(axis=1)
print(f'{sheet_name} 的 A、B、C、W、E 列的和为: {sum_columns.tolist()}')
```
希望对你有帮助!
python读取 excel 表格所有的sheet,获取 sheet 的所有列,判断列是否存在 A,B,C,W,E 列,如果都存在则对列行数进行求和,如果发现有不存在的列则去掉求和该列,将结果保存到新列 ‘盖码合并’ 中。
好的,这是一个关于 Python 读取 Excel 表格并进行一系列操作的问题。根据题意,可以使用 pandas 库中的 read_excel 方法读取 Excel 表格,然后使用 columns 属性获取所有列名,再使用 isin 方法判断 A、B、C、W、E 列是否存在,如果都存在,则使用 iloc 方法获取行数,进行求和,然后添加新列并将结果保存到新列中。具体实现可以搜索相关的代码示例。
相关推荐
![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)