osyysoosyyso
osyysoosyyso

Reputation: 11

Adding a function to a string in a pandas dataframe

I have a data frame that contains a column with countries. I want to convert the country names to capital cities. Example of how the function works:

from countryinfo import CountryInfo
    
CountryInfo('Lebanon').capital()

Would return Beirut

How can i do this?

Upvotes: 1

Views: 57

Answers (1)

Cdaman
Cdaman

Reputation: 320

you can use the lambda function and pd.apply() like this:

from countryinfo import CountryInfo
df['Capital'] = df['country'].apply(lambda x : CountryInfo(x).capital()

Here you can put your own df's column name in place of 'Capital' and 'country'.

Upvotes: 2

Related Questions