58

I have two dataframes with the same index but different columns. How do I combine them into one with the same index but containing all the columns?

I have:

  A 
1 10 
2 11

  B
1 20
2 21

and I need the following output:

  A  B
1 10 20
2 11 21
alexsalo
  • 1,406
  • 2
  • 14
  • 16

2 Answers2

113
pandas.concat([df1, df2], axis=1)
BrenBarn
  • 242,874
  • 37
  • 412
  • 384
20

You've got a few options depending on how complex the dataframe is:

Option 1:

df1.join(df2, how='outer')

Option 2:

pd.merge(df1, df2, left_index=True, right_index=True, how='outer')
unique_beast
  • 1,379
  • 2
  • 11
  • 23