我希望代码逐行合并输出的csv文件。它将依次写第一行,然后依次写第二行。
import numpy as np, pandas as pd, os, glob
path = (r'E:\csvfile')
all_files = glob.glob(path + "/*.csv")
li = []
for filename in all_files:
df = pd.read_csv(filename, index_col=False, header=0)
li.append(df)
frame = pd.concat(li,axis=0,names=None)
frame.to_csv (r'E:\csvfile\exportC.csv', mode = 'w', index=False)
我尝试了使用不同参数的较短代码。 >
import pandas as pd, glob
df = pd.concat(map(pd.read_csv, glob.glob(r'E:\csvfile/*.csv')),axis=0)
df.to_csv (r'E:\csvfile\exportC.csv',mode = 'w', index=False)
file1.csv
0, 10,12
0,11,12
1,15,12
file2.csv
0, 2, 1
1,22, 1
3, 11, 1
file3.csv
0, 4, 6
9, 14, 13
5, 6, 2
预期输出。
0, 10,12
1,22, 1
0, 4, 6
0,11,12
1,22, 1
9, 14, 13
1,15,12
3, 11, 1
5, 6, 2
谢谢你,从现在开始。
您可以先连接三个单独的数据框,然后使用sort_index
pandas方法根据索引(行)号重新组织数据框:
df=pd.concat([df1, df2, df3], axis=0).sort_index()