2017-10-19 173 views
1

行適用的功能行說我有這個DF:大熊貓由

sort_countries[["country", "Total"]].to_dict() 
{'Total': {'Afghanistan': 1, 
    'Albania': 1, 
    'Algeria': 1, 
    'Argentina': 6, 
    'Armenia': 5, 
    'Australia': 76, 
    'Austria': 9, 
    'Bangladesh': 1, 
    'Barbados': 1, 
    'Belarus': 12, 
    'Belgium': 27, 
    'Brazil': 34, 
    'Bulgaria': 2, 
    'Canada': 78, 
    'Chile': 3, 
    'China': 149, 
    'Colombia': 4, 
    'Costa Rica': 1, 
    'Croatia': 4, 
    'Czech Republic': 14, 
    'Denmark': 6, 
    'Ecuador': 1, 
    'Egypt': 4, 
    'El Salvador': 1, 
    'Estonia': 7, 
    'Finland': 18, 
    'France': 125, 
    'Germany': 106, 
    'Greece': 18, 
    'Hungary': 13, 
    'India': 145, 
    'Indonesia': 4, 
    'Iran': 6, 
    'Ireland': 10, 
    'Israel': 28, 
    'Italy': 31, 
    'Japan': 74, 
    'Kenya': 1, 
    'Kyrgyzstan': 1, 
    'Latvia': 2, 
    'Lebanon': 1, 
    'Lithuania': 7, 
    'Luxembourg': 1, 
    'Macedonia': 1, 
    'Madagascar': 2, 
    'Malaysia': 4, 
    'Mauritius': 1, 
    'Mexico': 3, 
    'Moldova': 1, 
    'Morocco': 2, 
    'Mozambique': 1, 
    'Nepal': 1, 
    'Netherlands': 59, 
    'New Zealand': 10, 
    'Nigeria': 1, 
    'North Korea': 1, 
    'Norway': 6, 
    'Pakistan': 1, 
    'Panama': 1, 
    'Philippines': 6, 
    'Poland': 51, 
    'Portugal': 15, 
    'Qatar': 1, 
    'Romania': 8, 
    'Russian Federation': 223, 
    'Senegal': 1, 
    'Serbia': 3, 
    'Singapore': 55, 
    'Slovakia': 8, 
    'Slovenia': 6, 
    'South Africa': 13, 
    'South Korea': 11, 
    'Spain': 52, 
    'Sri Lanka': 5, 
    'Sweden': 20, 
    'Switzerland': 24, 
    'Taiwan': 27, 
    'Thailand': 2, 
    'Turkey': 10, 
    'Ukraine': 40, 
    'United Arab Emirates': 2, 
    'United Kingdom': 117, 
    'United States': 852, 
    'Venezuela': 2, 
    'Vietnam': 5}, 
'country': {'Afghanistan': 'Afghanistan', 
    'Albania': 'Albania', 
    'Algeria': 'Algeria', 
    'Argentina': 'Argentina', 
    'Armenia': 'Armenia', 
    'Australia': 'Australia', 
    'Austria': 'Austria', 
    'Bangladesh': 'Bangladesh', 
    'Barbados': 'Barbados', 
    'Belarus': 'Belarus', 
    'Belgium': 'Belgium', 
    'Brazil': 'Brazil', 
    'Bulgaria': 'Bulgaria', 
    'Canada': 'Canada', 
    'Chile': 'Chile', 
    'China': 'China', 
    'Colombia': 'Colombia', 
    'Costa Rica': 'Costa Rica', 
    'Croatia': 'Croatia', 
    'Czech Republic': 'Czech Republic', 
    'Denmark': 'Denmark', 
    'Ecuador': 'Ecuador', 
    'Egypt': 'Egypt', 
    'El Salvador': 'El Salvador', 
    'Estonia': 'Estonia', 
    'Finland': 'Finland', 
    'France': 'France', 
    'Germany': 'Germany', 
    'Greece': 'Greece', 
    'Hungary': 'Hungary', 
    'India': 'India', 
    'Indonesia': 'Indonesia', 
    'Iran': 'Iran', 
    'Ireland': 'Ireland', 
    'Israel': 'Israel', 
    'Italy': 'Italy', 
    'Japan': 'Japan', 
    'Kenya': 'Kenya', 
    'Kyrgyzstan': 'Kyrgyzstan', 
    'Latvia': 'Latvia', 
    'Lebanon': 'Lebanon', 
    'Lithuania': 'Lithuania', 
    'Luxembourg': 'Luxembourg', 
    'Macedonia': 'Macedonia', 
    'Madagascar': 'Madagascar', 
    'Malaysia': 'Malaysia', 
    'Mauritius': 'Mauritius', 
    'Mexico': 'Mexico', 
    'Moldova': 'Moldova', 
    'Morocco': 'Morocco', 
    'Mozambique': 'Mozambique', 
    'Nepal': 'Nepal', 
    'Netherlands': 'Netherlands', 
    'New Zealand': 'New Zealand', 
    'Nigeria': 'Nigeria', 
    'North Korea': 'North Korea', 
    'Norway': 'Norway', 
    'Pakistan': 'Pakistan', 
    'Panama': 'Panama', 
    'Philippines': 'Philippines', 
    'Poland': 'Poland', 
    'Portugal': 'Portugal', 
    'Qatar': 'Qatar', 
    'Romania': 'Romania', 
    'Russian Federation': 'Russian Federation', 
    'Senegal': 'Senegal', 
    'Serbia': 'Serbia', 
    'Singapore': 'Singapore', 
    'Slovakia': 'Slovakia', 
    'Slovenia': 'Slovenia', 
    'South Africa': 'South Africa', 
    'South Korea': 'South Korea', 
    'Spain': 'Spain', 
    'Sri Lanka': 'Sri Lanka', 
    'Sweden': 'Sweden', 
    'Switzerland': 'Switzerland', 
    'Taiwan': 'Taiwan', 
    'Thailand': 'Thailand', 
    'Turkey': 'Turkey', 
    'Ukraine': 'Ukraine', 
    'United Arab Emirates': 'United Arab Emirates', 
    'United Kingdom': 'United Kingdom', 
    'United States': 'United States', 
    'Venezuela': 'Venezuela', 
    'Vietnam': 'Vietnam'}} 

和每個專欄中,我想註冊一個帶有ISO3代碼的另一列:

def get_3_code_country(country): 
    url = 'https://restcountries.eu/rest/v2/name/' + quote(country.country[0], safe='') 
    r = requests.get(url) 
    response = r.json() 
    return response[0]['alpha3Code'] 

import requests 
from urllib.parse import quote 

data_javascript_map = sort_countries.assign(
    iso_3=lambda x: get_3_code_country(x), axis = 1 
) 
data_javascript_map 

這顯然只返回第一國家,因爲我使用country.country [0]。但我想要讓所有國家一個接一個。

嘗試country.country.item()如其他答案建議,但沒有運氣。但是我得到的是整個系列,而不是1加1。在apply方法中,是否有一種方法可以一對一地執行,而不是整個系列,這樣我就可以避免在函數中執行for循環了?

+0

剛拿到它,應用數據幀,applymap工程逐元素的一個數據幀的行/列的基礎上工作,並且地圖作品元素方面的一個系列 –

回答

2

修改你的功能,例如:

def get_3_code_country(c): 
    url = 'https://restcountries.eu/rest/v2/name/' + quote(c, safe='') 
    ... 

現在,調用applydf.country

df['iso_3'] = df.country.apply(get_3_code_country) 

當心!這是一個requests操作,它將是


試用此功能時,我收到了KeyError。也許這些國家中有些不被支持?嘗試添加try-except支柱:

try: 
    return response[0]['alpha3Code'] 
except KeyError: 
    return None