# Unable to successfully merge dataframes in pandas along labels

I have two different dataframes, they both share the same labels, "Country" and "Year", I am trying to merge these together as one by these two columns.

This is my code:

joined = pd.merge(left = df, right = df1, on = ["Year", "Country"])


This is the result I receive for joined.head():

0 rows × 34 columns


Any suggestions?

Thanks.

Try the following:

new_df = pd.merge(df, df1, how='left', left_on=["Year", "Country"], right_on = ["Year", "Country"])

• Hi, I tried that, and indeed, it did align these two labels. However, now all of the other data in the columns are all NaN. Can you suggest anything to tweak this? Thanks for the help Oct 14, 2020 at 15:50
• It's difficult to say without seeing your data - but I would consult the following resource to make sure you're merging the way you want to: pandas.pydata.org/pandas-docs/stable/user_guide/merging.html Oct 14, 2020 at 15:54
• Forgot to thank you for this, you saved me a great deal of time. Your example had me thinking about the result your code gave and I solved it with a simple concat. Thanks for taking the time. Cheers. Oct 27, 2020 at 15:34