我想将目录中的多个csv文件读入pandas,并将它们连接成一个大的DataFrame。我还无法弄清楚。这是我到目前为止的内容:
csv
pandas
DataFrame
import glob import pandas as pd # get data file names path =r'C:\DRO\DCL_rawdata_files' filenames = glob.glob(path + "/*.csv") dfs = [] for filename in filenames: dfs.append(pd.read_csv(filename)) # Concatenate all data into one DataFrame big_frame = pd.concat(dfs, ignore_index=True)
我想我在for循环中需要一些帮助吗???
如果所有csv文件中的列均相同,则可以尝试以下代码。我已添加,header=0以便在读取csv第一行后可以将其分配为列名。
import pandas as pd import glob
path = r’C:\DRO\DCL_rawdata_files’ # use your path all_files = glob.glob(path + “/*.csv”)
li = []
for filename in all_files: df = pd.read_csv(filename, index_col=None, header=0) li.append(df)
frame = pd.concat(li, axis=0, ignore_index=True)