2017-10-06 58 views
1

Hej!國家數據:根據字典更名爲熊貓

我正在對國別統計數據進行一些數據分析。我現在使用來自不同來源的數據,並且很快就會看到,有些國家會有不同的呼叫方式:世界銀行稱它爲「英國和北愛爾蘭」,世界衛生組織稱之爲「英國」,意思是相同的政治建構(我意識到英格蘭,蘇格蘭和威爾士是「國家」,而不是英國)。

我創建了一本字典,我把大部分不同的名字都標準化爲世界銀行數據。這在列表中的作用就像一個魅力,但我需要它在一個熊貓DataFrame中,我從pd.read_csv得到。 在例如:如果我有一個很短的字典

dict = {'US': 'USA'} 

我怎麼可以在我的數據框(在df.country列設置爲dict.key值)內翻譯呢?

顯示它例如在:

ID country val 
1 US  some values 

到:

ID country val 
1 USA  some values 

對於我的錶轉換我用下面的結構,其中listB是輸入和輸出列表:

for key in dict: 
    listB = [w.replace(key, dict[key]) for w in listB] 

任何建議如何最容易地做到這一點?任何幫助都是極好的!

P.S:還有一點需要注意的是,有沒有人知道如何生成ISO 3166-1 alpha-3編碼(如德國= GER,瑞典= SWE等?)。這可能是上述問題的延伸。

回答

2

使用replace

df['country'] = df['country'].replace(dic) 

而對於ISO 3166-1阿爾法 - 3檢查answers

我認爲simpliest是從here下載。

如果想從wikipedia解析代碼可以使用this溶液或python 3 rewrited爲DataFrame

from bs4 import BeautifulSoup 
import requests 

url = "http://en.wikipedia.org/wiki/ISO_3166-1" 
r = requests.get(url) 
soup = BeautifulSoup(r.content, "lxml") 

t = soup.findAll('table', {'class' : 'wikitable sortable'})[1] 
L = [] 
cs = [th.findAll(text=True)[0] for th in t.findAll('th')] 

for row in t.findAll("tr")[1:]: 
    tds = row.findAll('td') 
    raw_cols = [td.findAll(text=True) for td in tds] 
    cols = [] 
    # country field contains differing numbers of elements, due to the flag -- 
    # only take the name 
    cols.append(raw_cols[0][-1:][0]) 
    # for all other columns, use the first result text 
    cols.extend([col[0] for col in raw_cols[1:]]) 
    L.append(cols) 

df = pd.DataFrame(L, columns=cs) 

print (df.head()) 
    English short name (upper/lower case) Alpha-2 code Alpha-3 code \ 
0       Afghanistan   AF   AFG 
1       Åland Islands   AX   ALA 
2        Albania   AL   ALB 
3        Algeria   DZ   DZA 
4      American Samoa   AS   ASM 

    Numeric code  Link to Independent 
0   004 ISO 3166-2:AF   Yes 
1   248 ISO 3166-2:AX   No 
2   008 ISO 3166-2:AL   Yes 
3   012 ISO 3166-2:DZ   Yes 
4   016 ISO 3166-2:AS   No 
+0

等你刮維基百科網頁和轉換爲'dic' – Dark

+0

:)不是那麼容易; 0 – jezrael

+0

https://gis.stackexchange.com/questions/1047/full-list-of-iso-alpha-2-and-iso-alpha-3-country-codes可能有幫助 – Dark