首页 > 解决方案 > 如何从指定目录中的多个 csv 文件创建单个数据框

问题描述

csv 文件自己放置在一个命名文件夹中。将 csv 文件中的所有数据读取到单个数据框中会很有用。到目前为止,这是一些代码:

#create a variable and look through contents of the directory 
files=[f for f in os.listdir("./your_directory") if f.endswith('.csv')]

#Initalize an empty data frame
all_data = pd.DataFrame()

#iterate through files and their contents, then concatenate their data into the data frame initialized above
for file in files:
   df = pd.read_csv('./your_directory' + file)
   all_data = pd.concat([all_data, df])

#Call the new data frame and verify that contents were transferred
all_data.head()

这给出了错误:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 4-5: truncated \UXXXXXXXX escape

如果有人让这个工作,你能展示一个工作的例子吗?

标签: pythoncsvjupyter-notebook

解决方案


这就是您使用所有代码的方式:

#create a variable and look through contents of the directory 
files=[f for f in os.listdir("./your_directory") if f.endswith('.csv')]

#Initalize an empty data frame
all_data = pd.DataFrame()

#iterate through files and their contents, then concatenate their data into the data frame initialized above
for file in files:
   df = pd.concat(map(pd.read_csv, glob.glob('./your_directory/*.csv)))
   all_data = pd.concat([all_data, df])

#Call the new data frame and verify that contents were transferred
all_data.head()

推荐阅读