Pandas逐行应用函数

时间:2017-10-19 11:30:11

标签: python pandas

说我有这个df:

ID

对于每一列,我想用iso3代码注册另一列:

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'}}

这显然只返回第一个国家,因为我使用country.country [0]。但我希望逐个获得所有国家。

按照其他答案中的建议尝试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 ,但没有运气。但是我得到了整个系列而不是1乘1.在apply方法中有一种方法可以逐个进行,而不是整个系列,这样我就可以避免在函数中执行for循环了吗?

1 个答案:

答案 0 :(得分:2)

修改你的功能:

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

现在,在apply上致电df.country

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

小心!这是requests操作,它将

我在尝试这个时收到KeyError。也许其中一些国家不受支持?尝试添加try-except大括号:

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