Home > Enterprise >  How could I use this function inside pandas with lambda
How could I use this function inside pandas with lambda

Time:05-05

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

CodePudding user response:

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) 

CodePudding user response:

Have a dictionary:

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

Then do this:

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

CodePudding user response:

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.

  • Related