简体   繁体   中英

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?

I've just recently started using Plotly and decided to give the "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 .) I made a small test set and applied the three digit codes to my data set and the map loaded up with all the data I put in, all within the correct countries I specified. This leads me to believe that choropleth looks in the data for the three digit country code by default in order to know where to place the data on the map.

Before I set something up that loops through the data we receive and changes all the codes to a 3 letter format from a 2 letter format, I was wondering if there is an easy way within Plotly choropleths that allows you to specify what type of country code you want?

Check the link below to get the mapping of 2 letter to 3 letter.

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'])

Now you can use the 3 Char country code. I was under a timeline so just took a short cut to it. Hope this helps.

Or you could use pycountry-convert package https://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)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM