【问题标题】: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 choropleth 使用 ISO 3166-1 alpha-2 国家代码而不是 ISO 3166-1 alpha-3 来映射国家?
【发布时间】:2016-07-22 09:59:55
【问题描述】:

我最近才开始使用 Plotly,并决定试一试"World Choropleth Map",因为它看起来很简单。我使用的数据集在指定受访者来自哪个国家/地区的列中有两位数的国家/地区代码(例如,BR 代表巴西,或 DE 代表德国,我相信也称为 ISO 3166-1 alpha-2 codes。)当我第一次设法运行该文件并使其在网络浏览器中加载,屏幕上没有出现地图,我觉得这很令人费解。我查看了示例 csv 并注意到它使用 three digit country codes(例如,意大利的 ITA 或瑞典的 SWE,这些被称为 ISO 3166-1 alpha 3 codes。)我制作了一个小型测试集并将三位数字代码应用于我的数据设置并且地图加载了我输入的所有数据,所有数据都在我指定的正确国家/地区。这让我相信 choropleth 默认会在数据中查找三位数的国家/地区代码,以便知道在地图上放置数据的位置。

在我设置循环接收的数据并将所有代码从 2 个字母格式更改为 3 个字母格式之前,我想知道在 Plotly choropleths 中是否有一种简单的方法可以让您指定什么类型你想要的国家代码?

【问题讨论】:

    标签: python plotly


    【解决方案1】:

    查看下面的链接以获取 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 个字符的国家/地区代码。我在一个时间表之下,所以只是走捷径。希望这会有所帮助。

    【讨论】:

    【解决方案2】:

    或者你可以使用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)))
    

    【讨论】:

      猜你喜欢
      • 2012-12-16
      • 2014-08-29
      • 2014-02-09
      • 2021-02-21
      • 1970-01-01
      • 2015-05-01
      • 2021-06-21
      • 2015-02-22
      • 2012-11-09
      相关资源
      最近更新 更多