我无法合并两个数据框。
第一个Df:国家/地区,具有201行和4列
In(1): country.head(10)
Out(1):
Country Code Population GDP per Capita
0 Afghanistan AFG 32526562.0 594.323081
1 Albania ALB 2889167.0 3945.217582
2 Algeria ALG 39666519.0 4206.031232
3 American Samoa* ASA 55538.0 NaN
4 Andorra AND 70473.0 NaN
5 Angola ANG 25021974.0 4101.472152
6 Antigua and Barbuda ANT 91818.0 13714.731960
7 Argentina ARG 43416755.0 13431.878340
8 Armenia ARM 3017712.0 3489.127690
9 Aruba* ARU 103889.0 NaN
第二个Df:G20,只有20行3列
In(2): g20.head(10)
Out(2):
Country HDI IMFClassification
0 Argentina 0.825 Emerging/Developing
1 Australia 0.939 Advanced
2 Brazil 0.759 Emerging/Developing
3 Canada 0.926 Advanced
4 China 0.752 Emerging/Developing
5 France 0.901 Advanced
6 Germany 0.936 Advanced
7 India 0.640 Emerging/Developing
8 Indonesia 0.694 Emerging/Developing
9 Italy 0.880 Advanced
合并尝试:
In(3):
#merge dataframes
for index, row in country.iterrows():
row['Country'] = row['Country'].strip()
for index, row in g20.iterrows():
row['Country'] = row['Country'].strip()
countriesDF = pd.merge(g20, country, how='left')
countriesDF
Out(3):
Country HDI IMFClassification Code Population GDP per Capita
0 Argentina 0.825 Emerging/Developing NaN NaN NaN
1 Australia 0.939 Advanced NaN NaN NaN
2 Brazil 0.759 Emerging/Developing NaN NaN NaN
3 Canada 0.926 Advanced NaN NaN NaN
4 China 0.752 Emerging/Developing NaN NaN NaN
5 France 0.901 Advanced NaN NaN NaN
6 Germany 0.936 Advanced NaN NaN NaN
7 India 0.640 Emerging/Developing NaN NaN NaN
8 Indonesia 0.694 Emerging/Developing NaN NaN NaN
9 Italy 0.880 Advanced NaN NaN NaN
10 Japan 0.909 Advanced NaN NaN NaN
11 Mexico 0.774 Emerging/Developing NaN NaN NaN
12 Russia 0.816 Emerging/Developing NaN NaN NaN
13 Saudi Arabia 0.853 Emerging/Developing NaN NaN NaN
14 South Africa 0.699 Emerging/Developing NaN NaN NaN
15 South Korea 0.903 Advanced NaN NaN NaN
16 Turkey 0.791 Emerging/Developing NaN NaN NaN
17 United Kingdom 0.922 Advanced NaN NaN NaN
18 United States 0.924 Advanced NaN NaN NaN
19 European Union 0.899 NaN NaN NaN NaN
在尝试将Country
上的国家/地区合并到g20时,最后3列的结尾均为空值,因此它们无法正确合并。我不确定问题可能出在实际数据集中。为什么会有空值?