Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge a list of dataframes to create one dataframe [duplicate]

I have a list of 18 data frames:

dfList = [df1, df2, df3, df4, df5, df6.....df18]

All of the data frames have a common id column so it's easy to join them each together with pd.merge 2 at a time. Is there a way to join them all at once so that dfList comes back as a single dataframe?

like image 446
Josh Avatar asked Aug 16 '16 14:08

Josh


1 Answers

I think you need concat, but first set index of each DataFrame by common column:

dfs = [df.set_index('id') for df in dfList]
print pd.concat(dfs, axis=1)

If need join by merge:

from functools import reduce
df = reduce(lambda df1,df2: pd.merge(df1,df2,on='id'), dfList)
like image 82
jezrael Avatar answered Nov 08 '22 20:11

jezrael