Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Change multiple column entries in a pandas dataframe

I have a large pandas dataframe

df.head()

        Year    Average Elo Club    Country Level
0       2017    1283.267334 Kukesi  ALB     0
1       2018    1263.912195 Kukesi  ALB     0
2       2019    1212.714661 Kukesi  ALB     0
3       2020    1231.063379 Kukesi  ALB     0
4       2018    1213.269553 Laci    ALB     0

The Country column contains abbreviated country codes for 55 countries. I want to change each of these country codes to the full name of the country (for example, ALB to Albania)

df['Country'].unique()

array(['ALB', 'AND', 'ARM', 'AUT', 'AZE', 'BEL', 'BHZ', 'BLR', 'BUL',
       'CRO', 'CYP', 'CZE', 'DEN', 'ENG', 'ESP', 'EST', 'FAR', 'FIN',
       'FRA', 'GEO', 'GER', 'GIB', 'GRE', 'HUN', 'IRL', 'ISL', 'ISR',
       'ITA', 'KAZ', 'KOS', 'LAT', 'LIE', 'LIT', 'LUX', 'MAC', 'MLT',
       'MNT', 'MOL', 'NED', 'NIR', 'NOR', 'POL', 'POR', 'ROM', 'RUS',
       'SCO', 'SLK', 'SMR', 'SRB', 'SUI', 'SVN', 'SWE', 'TUR', 'UKR',
       'WAL'], dtype=object)

I have written code that does this for ALB specifically:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df.Country[df.Country=='ALB'] = 'Albania'

Is there a way to do this for all 55 countries?

>Solution :

Yes, you can automate the process of replacing country codes with full names for all 55 countries. One way to achieve this is by using a dictionary that maps each country code to its corresponding full name. Here’s an example:

import pandas as pd

# Sample data
data = {
    'Year': [2017, 2018, 2019, 2020, 2018],
    'Average Elo Club': [1283.267334, 1263.912195, 1212.714661, 1231.063379, 1213.269553],
    'Country': ['ALB', 'AND', 'ARM', 'AUT', 'AZE'],
    'Level': [0, 0, 0, 0, 0]
}

df = pd.DataFrame(data)

# Country code to full name mapping
country_mapping = {
    'ALB': 'Albania',
    'AND': 'Andorra',
    'ARM': 'Armenia',
    # Add more country code to full name mappings here
    # For all 55 countries
}

# Replace country codes with full names
df['Country'] = df['Country'].replace(country_mapping)

print(df.head())

In the "country_mapping" dictionary, you can add mappings for all 55 countries, specifying the country code as the key and the corresponding full name as the value. When you call "df[‘Country’].replace(country_mapping)", it will replace the country codes in the "Country" column with the respective full names for all countries.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading