考虑到您已经拥有了数据集,或者可以通过pycountry获取数据集,您可以使用以下方法来处理它。在import pycountry
new_df = df['country-code'].apply(lambda x: pycountry.countries.get(alpha_3=x).name if len(x) == 3 else pycountry.countries.get(alpha_2=x).name)
print new_df
打印:
^{pr2}$
现在,考虑到长度2和长度3的代码都有csv,如下所示:df2
code name
0 AF Afghanistan
1 DE Germany
2 US United States
以及df3
code name
0 BEL Belgium
1 AUS Australia
2 IND India
3 GBR United Kingdom
在此之后,请执行以下步骤:>>> new_df2 = df.merge(df2, left_on='country_code', right_on='code')
>>> new_df2
amount country_code code name
0 100 AF AF Afghanistan
1 400 DE DE Germany
2 125 US US United States
>>> new_df3 = df.merge(df