Используйте merge
с левым соединением или map
:
df3 = df.merge(df2[['Country','GDP (PPP)']], how='left')
print (df3.head())
Country TFR GDP (PPP)
0 Afghanistan 5.12 $2,000
1 Albania 1.51 $12,500
2 Algeria 2.70 $15,200
3 American Samoa 2.68 $11,200
4 Andorra 1.40 $49,900
df['GDP (PPP)'] = df['Country'].map(df2.set_index('Country')['GDP (PPP)'])
print (df.head())
Country TFR GDP (PPP)
0 Afghanistan 5.12 $2,000
1 Albania 1.51 $12,500
2 Algeria 2.70 $15,200
3 American Samoa 2.68 $11,200
4 Andorra 1.40 $49,900
Если значения в стране от df2['Country']
несуществует в df['Country']
создан NaN
:
print (df[df['GDP (PPP)'].isna()])
Country TFR GDP (PPP)
43 Christmas Island NaN NaN
44 Cocos (Keeling) Islands NaN NaN
78 Gaza Strip 4.13 NaN
154 Norfolk Island NaN NaN
165 Pitcairn Islands NaN NaN
191 Somalia 5.80 NaN
198 Svalbard NaN NaN
230 World 2.42 NaN