我试图创建一个脚本来迭代文件夹中的.csv文件,然后在保存到新的.csv文件之前对它们运行一些计算。我已经能够在生成均值和百分比时使其正常工作,但在尝试添加某些条件时存在KeyError问题。
以下是我到目前为止编写的代码:
import os
import pandas as pd
import csv
folder_path = 'D:/Libraries/Documents/data'
rtAvg = 'block1_respo.rt'
cond = 'block1_trigger'
output_file = 'results_compiled.csv'
df_list = []
for filename in os.listdir(folder_path):
if filename.endswith('.csv'):
# Load the CSV file into a data frame
df = pd.read_csv(os.path.join(folder_path, filename))
# This code will check if the column is missing, and will populate that row with "9999" if it is
if rtAvg not in df.columns:
df[rtAvg] = 9999
rt_mean_b1 = df[rtAvg].mean()
con1 = df.loc[df[cond]==101][rtAvg].mean()
con2 = df.loc[df[cond]==102][rtAvg].mean()
con3 = df.loc[df[cond]==103][rtAvg].mean()
con4 = df.loc[df[cond]==104][rtAvg].mean()
# Create a new row for the summary data
summary_row = pd.DataFrame({
'csv_file': filename,
'rt_average': rt_mean_b1,
'101_rt':con1,
'102_rt':con2,
'103_rt':con3,
'104_rt':con4
},index = [0])
# Append the summary data to the list of data frames
df_list.append(summary_row)
summary_df = pd.concat(df_list)
summary_df.to_csv( output_file, index = False)
以下是我收到的错误代码:
con1 = df.loc[df[cond]==101][rtAvg].mean()
File "C:\Program Files\PsychoPy\lib\site-packages\pandas\core\frame.py", line 3458, in __getitem__
indexer = self.columns.get_loc(key)
File "C:\Program Files\PsychoPy\lib\site-packages\pandas\core\indexes\base.py", line 3363, in get_loc
raise KeyError(key) from err
KeyError: 'block1_trigger'
block1_trigger确实存在。我已经打印了列表,它就在那里。我以前也试过用代码删除白色,但没有什么区别。我还试过cond = ' block1_trigger'和'block1_trigger ',都没有产生预期的结果。
1条答案
按热度按时间nzk0hqpo1#
我已经找出了错误。一些文件丢失了这一列。我现在已经添加了一个catch代码来排序。