[英]Error when trying to read multiple .csv files in Jupyter Notebook using python
I am given a file that contains 1000.csv files(data0,data1,data2..........,data999) and I need to read all those files.我得到一个包含 1000.csv 文件(data0,data1,data2........,data999)的文件,我需要读取所有这些文件。 So, I tried it on my own.所以,我自己试了一下。 This was my approach: read data0.csv and perform transpose on it and then loop it through all the data*.csv files and then append them.这是我的方法:读取 data0.csv 并对其执行转置,然后遍历所有 data*.csv 文件,然后是 append 文件。 But I was getting an error.但我遇到了一个错误。 Could someone help me out?有人可以帮我吗? Reading data0.csv file and transposing it:读取 data0.csv 文件并转置:
df = pd.read_csv('data0.csv')
print (df.head(10))
df_temp = df
df_main = df_temp.transpose()
df_main
new_df = [df_main]
for i in range(1000):
filename = "data%d.csv"%i
df_s = pd.read_csv(filename)
new_df= pd.concat([df_s])
new_df[1]
looping through 1000 files, transposing and concating:循环遍历 1000 个文件,转置和连接:
after transposing and appending all the 1000 csv files I should be getting 1000 rows x 150 columns.在转置和附加所有 1000 个 csv 文件后,我应该得到 1000 行 x 150 列。 But I am not getting that.但我不明白。
I couldn't test this, because you did not provide an example of your file as text.我无法对此进行测试,因为您没有以文本形式提供文件示例。 Please try to provide a minimal reproducible example next time.下次请尝试提供一个最小的可重现示例。
My solution is a minor variation of this SO post mentioned by @Ranika Nisal.我的解决方案是@Ranika Nisal 提到的这篇 SO 帖子的一个小变体。
dfs = [pd.read_csv(f'data{i}.csv') for i in range(1000)]
df = pd.concat(dfs, axis=0, ignore_index=True)
Your solution did not generate a list of dataframes which is required for pd.concat() to work.您的解决方案没有生成 pd.concat() 工作所需的数据帧列表。 Also, you tried to access the second dataframe with new_df[1]
but there was only one element in your list.此外,您尝试使用new_df[1]
访问第二个 dataframe 但列表中只有一个元素。 That's the reason why you've received a KeyError
.这就是您收到KeyError
的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.