1

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?

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.