I have two data frames df1 and df2 and I would like to merge them into a single data frame. It is as if df1 and df2 were created by splitting a single data frame down the center vertically, like tearing a piece of paper that contains a list in half so that half the columns go on one paper and half the columns go on the other. I would like to merge them back together. How do I do it?


Pandas has a built-in merge function. Please refer to the documentation

End-result should be something like this:
df_merged = pd.merge(df1, df2, left_on=['name_indexcolumn_df1_here'],

If you split the DataFrame "vertically" then you have two DataFrames that with the same index.

You can use the merge function or the concat function.

With concat with would be something like this:

pandas.DataFrame.concat([df1,df2], axis=1)

With merge with would be something like this:

pandas.Dataframe.merge([df1,df2], left_index=True)

For more complex merging options see the Merge, join and concat pandas tutorial.

  • 1
    $\begingroup$ merge is wrong... have you tried running the code? $\endgroup$ – Jorge Leitao Aug 18 '17 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.