import pandas as pd # 使用pandas库读取Excel文件中的数据表 data = pd.read_excel('data.xlsx', sheet_name='Sheet1') # 选择原因列的数据 cause_column = data.iloc[:, 4] # 遍历原因列的数据,将每行数据添加到一个列表中 conses = [] for cause in cause_column: conses.append(cause) a='酸性' b='低压' count=0 for cause in conses: if a in cause and b in cause: count += 1 print(count)
时间: 2024-03-14 18:48:13 浏览: 22
根据你提供的代码,可能出现 TypeError 是因为原因列中包含了浮点类型的数据,而你尝试将其作为字符串进行迭代。你可以使用 isinstance() 函数来判断原因列中数据的类型,例如:
```
for cause in cause_column:
if isinstance(cause, str):
conses.append(cause)
```
这样可以确保只有字符串类型的数据被添加到列表中。另外,你还可以检查变量 a 和 b 是否是字符串类型的对象,如果不是,可以使用 str() 函数将其转换为字符串类型。
相关问题
import os import pandas as pd # 设置目标文件夹路径 folder_path = r"D:/拆分后的工作簿" # 获取文件夹下所有的Excel文件 excel_files = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if f.endswith('.xlsx')] # 创建一个空的DataFrame combined_data = pd.DataFrame() # 循环读取Excel文件,并将所有工作表合并到一个DataFrame中 for file in excel_files: print('Processing file:', file) excel_data = pd.read_excel(file, sheet_name=None) for sheet_name, sheet_data in excel_data.items(): sheet_data['Sheet'] = sheet_name combined_data = pd.concat([combined_data, sheet_data], ignore_index=True) # 将合并的数据写入一个Excel文件 writer = pd.ExcelWriter('combined_workbook.xlsx', engine='xlsxwriter') combined_data.to_excel(writer, index=False) writer.save()怎麽修改
你可以尝试将代码修改为以下形式,使用`with`语句来自动关闭文件:
```python
import os
import pandas as pd
# 设置目标文件夹路径
folder_path = r"D:/拆分后的工作簿"
# 获取文件夹下所有的Excel文件
excel_files = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if f.endswith('.xlsx')]
# 创建一个空的DataFrame
combined_data = pd.DataFrame()
# 循环读取Excel文件,并将所有工作表合并到一个DataFrame中
for file in excel_files:
print('Processing file:', file)
excel_data = pd.read_excel(file, sheet_name=None)
for sheet_name, sheet_data in excel_data.items():
sheet_data['Sheet'] = sheet_name
combined_data = pd.concat([combined_data, sheet_data], ignore_index=True)
# 将合并的数据写入一个Excel文件
with pd.ExcelWriter('combined_workbook.xlsx', engine='xlsxwriter') as writer:
combined_data.to_excel(writer, index=False)
```
在这个修改后的代码中,我们使用`with`语句来创建ExcelWriter对象,并在代码块结束时自动关闭该对象。这样可以避免忘记手动关闭ExcelWriter对象而导致的文件无法保存的问题。
import pandas as pd # 读取源Excel文件 df = pd.read_excel('kpi_evaluation202201.xlsx', sheet_name='用户打分') # 选择指定的列和行 df_selected = df.iloc[2:, 0:16] # 选择第3行及以下和第1到第6列的数据 # 导出到ExcelB文件并从第二行开始存放数据 df_selected.to_excel('execl_total_data.xlsx', index=False, startrow=1)将这个程序修改一下:修改成---从exec了文件kpi_evaluation202201.xlsx、kpi_evaluation202202.xlsx、kpi_evaluation202203.xlsx、kpi_evaluation202204.xlsx、kpi_evaluation202205.xlsx、kpi_evaluation202206.xlsx中读取文件
你可以使用循环来读取多个文件,并将它们合并为一个数据框。以下是修改后的代码示例:
```python
import pandas as pd
# 文件列表
file_list = ['kpi_evaluation202201.xlsx', 'kpi_evaluation202202.xlsx', 'kpi_evaluation202203.xlsx', 'kpi_evaluation202204.xlsx', 'kpi_evaluation202205.xlsx', 'kpi_evaluation202206.xlsx']
# 创建一个空的数据框
df_merged = pd.DataFrame()
# 循环读取文件并合并数据
for file in file_list:
df = pd.read_excel(file, sheet_name='用户打分')
df_selected = df.iloc[2:, 0:16]
df_merged = pd.concat([df_merged, df_selected])
# 导出到Excel文件并从第二行开始存放数据
df_merged.to_excel('execl_total_data.xlsx', index=False, startrow=1)
```
这样,代码将从每个文件中读取数据,并将它们合并为一个数据框。最后,将合并的数据框导出到一个名为 "execl_total_data.xlsx" 的 Excel 文件中,并从第二行开始存放数据。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)