39
votes

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
2

2 Answers

84
votes
pandas.concat([df1, df2], axis=1)
14
votes

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')