簡體   English   中英

是否可以讓 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 並決定試一試“世界等值線地圖”,因為它看起來很簡單。 我使用的數據集在指定受訪者來自哪個國家的列中具有兩位數的國家/地區代碼(例如,BR 代表巴西,DE 代表德國,也稱為ISO 3166-1 alpha-2 代碼,我相信.) 當我第一次設法運行該文件並將其加載到 Web 瀏覽器中時,屏幕上沒有顯示任何地圖,這讓我感到非常困惑。 我查看了示例 csv 並注意到它使用三位數的國家代碼(例如,意大利的 ITA 或瑞典的 SWE,這些被稱為ISO 3166-1 alpha 3 代碼。)我做了一個小測試集並應用了三個數字代碼添加到我的數據集,地圖加載了我輸入的所有數據,所有數據都在我指定的正確國家/地區內。 這讓我相信 choropleth 在默認情況下會查看三位國家/地區代碼的數據,以便知道將數據放置在地圖上的位置。

在我設置循環遍歷我們收到的數據並將所有代碼從 2 個字母格式更改為 3 個字母格式之前,我想知道 Plotly choropleths 中是否有一種簡單的方法可以讓您指定什么類型的國家/地區代碼你想要嗎?

檢查下面的鏈接以獲取 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)))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM