[英]How to convert country code to name with country_converter, in pandas?
df
Date Value CODE
0 2020-03-12 7 AFG
1 2020-03-11 7 AFG
...
我怎么能从代码中生成国家? 我试过哪个只是从 CODE 变量生成代码
import country_converter as coco
df['country'] = df['CODE'].apply(coco.convert)
Actual Output:
Date Value CODE country
0 2020-03-12 7 AFG AFG
1 2020-03-11 7 AFG AFG
Expected Output:
Date Value CODE country
0 2020-03-12 7 AFG Afghanistan
1 2020-03-11 7 AFG Afghanistan
to
和src
参数的有效值可以通过coco.CountryConverter().valid_class
。import country_converter as coco
import pandas as pd
# setupt test dataframe
df = pd.DataFrame({'code': ['AFG', 'USA', 'RU']})
# add country name by applying the convert method
df['short name'] = df.code.apply(lambda x: coco.convert(names=x, to='name_short', not_found=None))
# display(df)
code short name
0 AFG Afghanistan
1 USA United States
2 RU Russia
# converting the column to a list and passing it to the method also works to add the short name
df['short name'] = coco.convert(names=df.code.tolist(), to='name_short', not_found=None)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.