简体   繁体   中英

How could I use this function inside pandas with lambda

I have a problem. I have a dataframe, that contains ISO-codes of countries. I want to change these ISO-codes to country names. But unfortunately I do not know how I could use these functions inside a .apply(lambda x:) function.

Dataframe

   id country
0   1      DE
1   2      DE
2   3      CN
3   4      BG
4   3      CN
5   4      BG
6   5      BG

Code

import pandas as pd
import pycountry

input_countries = ['BG', 'CN', 'DE']

countries = {}
for country in pycountry.countries:
    countries[country.alpha_2] = country.name

codes = [countries.get(country, 'Unknown code') for country in input_countries]

import pandas as pd
d = {'id': [1, 2, 3, 4, 3, 4, 5], 'country': ['DE', 'DE', 'CN', 'BG', 'CN', 'BG', 'BG']}
df = pd.DataFrame(data=d)
# print(df)


df['country'] = df['country'].apply(lambda x: ...)

What I want

   id country
0   1      Germany
1   2      Germany
2   3      China
3   4      Bulgaria
4   3      China
5   4      Bulgaria
6   5      Bulgaria

The most suitable function to use here is probably map instead, for the 'country' column. Pseudo code as below:

country_map = dict(zip(country_ids, country_names))
df['country'] = df['country'].map(country_map)

where country_ids and country_names are the lists or columns of the input codes and the desired output country names.

I think you should use df.apply instead of df['country'].apply to create new function from the given value in country column

import pandas as pd
import pycountry

input_countries = ['BG', 'CN', 'DE']

countries = {}
for country in pycountry.countries:
    countries[country.alpha_2] = country.name

codes = [countries.get(country, 'Unknown code') for country in input_countries]

import pandas as pd
d = {'id': [1, 2, 3, 4, 3, 4, 5], 'country': ['DE', 'DE', 'CN', 'BG', 'CN', 'BG', 'BG']}
df = pd.DataFrame(data=d)
# print(df)


df['country'] = df.apply(lambda x: countries[x['country']], axis=1) 

Have a dictionary:

d ={
'DE': 'Germany'.
 ...
}

Then do this:

df['country'] = df['country'].apply(lambda x: d[x['country']])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM