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
如果有人让它起作用,您可以举一个有效的例子吗?
答案 0 :(得分:0)
import glob
import pandas as pd
df = pd.concat(map(pd.read_csv, glob.glob('./your_directory/*.csv')))
答案 1 :(得分:0)
这是您使用所有代码的方式:
#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()