是否可以让 Plotly choropleth 使用 ISO 3166-1 alpha-2 国家代码而不是 ISO 3166-1 alpha-3 来映射国家?

Is it possible to make Plotly choropleth use ISO 3166-1 alpha-2 country codes instead of ISO 3166-1 alpha-3 to map countries?

我最近才开始使用 Plotly 并决定提供 "World Choropleth Map" a go due to how simple it seems to be. I was using a data set that has two digit country codes in the column that specifies what country the respondent is from (for example, BR for Brazil, or DE for Germany, also known as ISO 3166-1 alpha-2 codes, I believe.) When I first managed to run the file and make it load up in a web browser, no map appeared on the screen, which I found quite puzzling. I looked at the example csv and noticed that it uses three digit country codes (for example, ITA for Italy or SWE for Sweden, these are known as ISO 3166-1 alpha 3 codes。)我制作了一个小测试集并将三位数代码应用于我的数据集,地图加载了所有数据我输入了,都在我指定的正确国家之内。这让我相信 choropleth 默认会查看三位数国家代码的数据,以便知道将数据放置在地图上的什么位置。

在我设置一些循环遍历我们收到的数据并将所有代码从 2 字母格式更改为 3 字母格式之前,我想知道 Plotly choropleths 中是否有一种简单的方法允许您指定您想要哪种类型的国家/地区代码?

检查下面的 link 以获得 2 个字母到 3 个字母的映射。

http://www.nationsonline.org/oneworld/country_code_list.htm

Countrylet     2let  3let  
Afghanistan    AF    AFG  
Aland Islands  AX    ALA 

dfcountry = pd.read_csv('countryMap.txt',sep='\t') # mapping of 2 letter to 3 letter

    Country  min    avg     max
    AD       5251   5251    5251

dfData = pd.read_csv('myData.txt',sep='\t') # My Source data

df = dfData.merge(dfcountry,how='inner',left_on=['Country'],right_on=['2let'])

现在您可以使用 3 个字符的国家/地区代码。我在一个时间表之下,所以只是走了捷径。希望这有帮助。

或者您可以使用 pycountry-converthttps://pypi.org/project/pycountry-convert/

from pycountry_convert import country_alpha2_to_country_name, country_name_to_country_alpha3

data['country_alpha_3'] = data.country_alpha_2.apply(lambda x: country_name_to_country_alpha3(country_alpha2_to_country_name(x)))