63

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?

Fabio Lamanna
  • 20,504
  • 24
  • 90
  • 122
Josh
  • 1,237
  • 4
  • 15
  • 22

1 Answers1

114

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)
Jinhua Wang
  • 1,679
  • 1
  • 17
  • 44
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 7
    `pd.concat(dfList, axis=1)` combined them all, but each df had it's own line (so every id was repeated 18 times) – Josh Aug 16 '16 at 15:00
  • 1
    I tried to write as variable: `sdf = pd.concat(dfs, axis=1)` and got `TypeError: first argument must be an iterable of pandas objects, you passed an object of type "DataFrame"` Don't get it. – Peter.k Feb 22 '19 at 16:59