我有一个目录里有n个文件,需要合并成一个文件。它们具有相同的列数,例如,test1.csv
的内容如下:
test1,test1,test1
test1,test1,test1
test1,test1,test1
同样,
test2.csv
文件的内容如下:test2,test2,test2
test2,test2,test2
test2,test2,test2
我希望final.csv看起来像这样:
test1,test1,test1
test1,test1,test1
test1,test1,test1
test2,test2,test2
test2,test2,test2
test2,test2,test2
但实际上它的输出结果是这样的:
test file 1,test file 1.1,test file 1.2,test file 2,test file 2.1,test file 2.2
,,,test file 2,test file 2,test file 2
,,,test file 2,test file 2,test file 2
test file 1,test file 1,test file 1,,,
test file 1,test file 1,test file 1,,,
有人可以帮我弄清楚这里发生了什么吗?我在下面粘贴了我的代码:
import csv
import glob
import pandas as pd
import numpy as np
all_data = pd.DataFrame() #initializes DF which will hold aggregated csv files
for f in glob.glob("*.csv"): #for all csv files in pwd
df = pd.read_csv(f) #create dataframe for reading current csv
all_data = all_data.append(df) #appends current csv to final DF
all_data.to_csv("final.csv", index=None)