简体   繁体   English

尝试使用 python 在 Jupyter Notebook 中读取多个.csv 文件时出错

[英]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.

相关问题 使用 read_csv 在 Jupyter Notebook 中创建相对路径时出错 - Error on creating relative path in Jupyter Notebook using read_csv 尝试启动 Jupyter Notebook (Python) 时出现运行时错误 - Runtime Error when trying to launch Jupyter Notebook (Python) 使用Jupyter Notebook将CSV文件加载到数据框时出现错误 - Getting error when loading CSV file to dataframe using Jupyter notebook 使用Jupyter Notebook中的Python语言读取文件夹中的所有jpeg图像文件时发生操作系统错误 - OS Error occurred when reading all the jpeg image files in a folder by using Python language in Jupyter Notebook 尝试在 jupyter 上使用 pandas 读取 csv 文件时出现解析器错误 - Parser Error when trying to read csv File with pandas on jupyter 当我的 csv 文件格式正确时,如何解决“模拟数组必须包含数值”错误?,使用 Jupyter Notebook - How to Solve "simulations array must contain numerical values" error when my csv files are already in proper format?, using Jupyter Notebook 尝试启动 Jupyter 笔记本时出现 DLL 错误 - DLL Error when trying to launch Jupyter notebook 尝试在 jupyter notebook 中拆分数据时出错 - Error when trying to split the data in jupyter notebook 使用 Jupyter 笔记本将具有多张工作表的 Excel 文件转换为多个 csv 文件 - Converting Excel file with multiple sheets into multiple csv files using Jupyter notebook 有没有办法在 jupyter notebook 中使用 insecureclient 将 hdfs 中的 csv 文件读入 python 数据帧? - Is there a way to read a csv file in hdfs into a python dataframe using insecureclient in a jupyter notebook?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM