我想将多个csv文件合并为一个df。

它们都是这种通用格式,带有两个索引列:

                                           1     2
CU0112-005287-7 Output Energy, (Wh/h)   0.064   0.066
CU0112-005287-7 Lights (Wh)                0     0

                                            1     2
CU0112-001885-L Output Energy, (Wh/h)   1.33    1.317
CU0112-001885-L Lights (Wh)             1.33    1.317


等等...

合并的df为:

                                           1     2
CU0112-005287-7 Output Energy, (Wh/h)   0.064   0.066
CU0112-005287-7 Lights (Wh)                0     0
CU0112-001885-L Output Energy, (Wh/h)   1.33    1.317
CU0112-001885-L Lights (Wh)             1.33    1.317


我正在尝试此代码:

import os
import pandas as pd
import glob

files = glob.glob(r'2017-12-05\Aggregated\*.csv')   //folder which contains all the csv files

df = pd.merge([pd.read_csv(f, index_col=[0,1])for f in files], how='outer')

df.to_csv(r'\merged.csv')


但我收到此错误:

TypeError: merge() takes at least 2 arguments (2 given)

最佳答案

我认为您需要concat而不是merge

df = pd.concat([pd.read_csv(f, index_col=[0,1]) for f in files])

关于python - Pandas 结合了多个csv文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49111093/

10-12 15:00