Home > OS >  How can I have the same index for two different columns where the columns do not have unqiue values?
How can I have the same index for two different columns where the columns do not have unqiue values?

Time:09-26

df1:

name_d name_o year
Turkiye Italy 1990
Turkiye Italy 1991
Turkiye Italy 1993
Spain Italy 1990
Spain Italy 1991
Spain Japan 1990

df2:

country_name year v2x_regime
Spain 1990 0
Turkiye 1990 0
Italy 1990 1
Turkiye 1991 1
Spain 1991 1
Italy 1991 1

Expected result:

name_o v2x_regime_name_o name_d v2x_regime_name_d year
Italy 1 Turkiye 1 1990

Basically I would like to know the regime type of the each country for each year. Since this is a bilateral data, there are two columns that include country name. For example, for each year I would like to have the index for name_o column and name_d column.

CodePudding user response:

Does this work for you?:

df = df1.merge(df2, left_on=['name_o','year'], right_on=['country_name','year'], how='left')
df = df.merge(df2, left_on=['name_d,'year'], right_on=['country_name','year'], how='left')
  • Related