0

df1 and df2 image

df1 = dataframe on the left, df2 = dataframe on the right.

I would like to append iso_alpha and iso_num from df2 to df 1 with condition if df1['Country_Name'] == df2['country'].

The result should look like below.

Country | Year | Month | guest_1 | iso_alpha |iso_num
The Netherlands | 2012 | Jan | 735 | NLD |528

Df2 image

I try with the code below but it getting error message.

new_df = sub.loc[sub['Country_Name'] == df1['country'], df1[:,-2]]

error = ValueError: Can only compare identically-labeled Series objects

Could you please advise how to solve this issue.

Popion
  • 1
  • 1

1 Answers1

1

What you want to do is to merge the DataFrame.

pd.merge(left, right, how='inner', on=None, left_on=None, right_on=None,
         left_index=False, right_index=False, sort=True,
         suffixes=('_x', '_y'), copy=True, indicator=False,
         validate=None)

https://pandas.pydata.org/pandas-docs/stable/user_guide/merging.html