gpt4 book ai didi

python - Pandas 逐行应用函数

转载 作者:行者123 更新时间:2023-12-01 02:30:53 26 4
gpt4 key购买 nike

假设我有这个 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 by 1。apply 方法中有没有一种方法可以 1 by 1 而不是整个系列,这样我就可以避免在我的函数中执行 for 循环?

最佳答案

修改您的函数:

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

现在,在 df.country 上调用 apply:

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

当心!这是一个请求操作,它会

<小时/>

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

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

关于python - Pandas 逐行应用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46828879/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com