如何将 IOC 国家代码转换为国家名称?
How do I convert an IOC country code to country name?
我有一个 pandas 数据框
import pandas as pd
s = pd.DataFrame({'ioc' : ['ESP', 'CYP', 'USA', 'ESP', 'NED']})
我想 return
out = pd.DataFrame(
{'ioc' : ['ESP', 'CYP', 'USA', 'ESP', 'NED'],
'countryName' : ['Spain', 'Cyprus', 'United States of America',
'Spain', 'Netherlands']})
import pycountry
def foo(a):
country = pycountry.countries.get(alpha_3=a)
return country.name if country else "NA"
s.ioc.apply(foo)
0 Spain
1 Cyprus
2 United States
3 Spain
4 NA
Name: ioc, dtype: object
另一种选择
import country_converter as coco
cc = coco.CountryConverter()
cc.convert(names=s.ioc, to='name')
# ['Spain', 'Cyprus', 'United States', 'Spain', 'not found']
您还可以从 country_converter 存储库中读取数据。
import json
import urllib.request
with urllib.request.urlopen(
"https://raw.githubusercontent.com/ohitsdaniel/country./master/lib/data/masterData.json"
) as url:
data = json.loads(url.read().decode())
d = {
x.get("ioc"): x.get("names")["en"][0]
for x in data["code"]["ISO2"].values()
if x.get("ioc")
}
s.ioc.apply(lambda x: d.get(x, "NA"))
0 Spain
1 Cyprus
2 United States
3 Spain
4 Netherlands
Name: ioc, dtype: object
ioc = pd.read_html('https://en.wikipedia.org/wiki/List_of_IOC_country_codes')[0]
ioc = ioc.assign(Code=ioc['Code'].str[-3:]).set_index('Code')['National Olympic Committee']
s['countryName'] = s['ioc'].map(ioc)
print(s)
# Output:
ioc countryName
0 ESP Spain
1 CYP Cyprus
2 USA United States
3 ESP Spain
4 NED Netherlands
我有一个 pandas 数据框
import pandas as pd
s = pd.DataFrame({'ioc' : ['ESP', 'CYP', 'USA', 'ESP', 'NED']})
我想 return
out = pd.DataFrame(
{'ioc' : ['ESP', 'CYP', 'USA', 'ESP', 'NED'],
'countryName' : ['Spain', 'Cyprus', 'United States of America',
'Spain', 'Netherlands']})
import pycountry
def foo(a):
country = pycountry.countries.get(alpha_3=a)
return country.name if country else "NA"
s.ioc.apply(foo)
0 Spain
1 Cyprus
2 United States
3 Spain
4 NA
Name: ioc, dtype: object
另一种选择
import country_converter as coco
cc = coco.CountryConverter()
cc.convert(names=s.ioc, to='name')
# ['Spain', 'Cyprus', 'United States', 'Spain', 'not found']
您还可以从 country_converter 存储库中读取数据。
import json
import urllib.request
with urllib.request.urlopen(
"https://raw.githubusercontent.com/ohitsdaniel/country./master/lib/data/masterData.json"
) as url:
data = json.loads(url.read().decode())
d = {
x.get("ioc"): x.get("names")["en"][0]
for x in data["code"]["ISO2"].values()
if x.get("ioc")
}
s.ioc.apply(lambda x: d.get(x, "NA"))
0 Spain
1 Cyprus
2 United States
3 Spain
4 Netherlands
Name: ioc, dtype: object
ioc = pd.read_html('https://en.wikipedia.org/wiki/List_of_IOC_country_codes')[0]
ioc = ioc.assign(Code=ioc['Code'].str[-3:]).set_index('Code')['National Olympic Committee']
s['countryName'] = s['ioc'].map(ioc)
print(s)
# Output:
ioc countryName
0 ESP Spain
1 CYP Cyprus
2 USA United States
3 ESP Spain
4 NED Netherlands