Combine two Pandas dataframes with the same index

Question:

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
Asked By: alexsalo

||

Answers:

pandas.concat([df1, df2], axis=1)
Answered By: BrenBarn

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')
Answered By: unique_beast
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.