简体   繁体   中英

merge csv files - python

I am using a for loop to merge csv files on jupyter notebook, however my result returns a list instead of a dataframe. Could someone help me and tell me what I am doing wrong? Thank you in advance.

files = ['babd_light_z1.csv','babd_light_z2.csv','babd_light_z3.csv']
data = []
for f in files:
     data.append(pd.read_csv(f))

type(data) # returns  list

You can simply use pd.concat(data, axis=0, ignore_index=True) outside your loop to merge your csv files as in:

files = ['babd_light_z1.csv', 'babd_light_z2.csv', 'babd_light_z3.csv']

data = []

for f in files:
    data.append(pd.read_csv(f))

df = pd.concat(data, axis=0, ignore_index=True)

type(df) should return pandas.core.frame.DataFrame

Give this a shot:

combined = pd.combine(data, axis=0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM