Converting ISO2 code by Python basic country to continental name

Asked 2 weeks ago, Updated 2 weeks ago, 1 views

Hello.

In order to convert the country-specific ISO2 code to a continental name, try for 5 hours and then post a question because it gets stuck.

First of all, I modified the code that I got through Google as below and succeeded.

# Get 'ISO2' code, which is sustituting 'Country' column, for merging w/ other df
!pip install pycountry
import pycountry

def get_country_code(name):
    for co in list(pycountry.countries):
        if name in co.name:
            return co.alpha_2
    return None

iso2_list = []
for name in country_list: # Using existing list
  iso2_list.append(get_country_code(name))
print(iso2_list)

# # Make a new column filled with 'ISO2' list
df_country['ISO2'] = iso2_list
df_country.head()

However, in the process of using this to obtain continental information using the above iso2_list along with the package called pycountry_converter, it is not successful because it is not easy to use def and for loop syntax together.

The code you want to use is as follows.

import pycountry_convert as pc
country_alpha2_to_continent_code()

Also, the code I tried is as follows.

# 1. Making continent code column by pycountry package
# # Renew definition of the list with filling empties
iso2_list = df_country['ISO2'].to_list()
## ## I think this way isn't working for "list" type
def get_continent_code(code):
    for co in iso2_list:
        if code in co.alpha_2:
            return pc.country_alpha2_to_continent_code(code)

continent_list = []
for code in iso2_list:
    continent_list.append(get_continent_code(code))
print(continent_list)
#pc.country_alpha2_to_continent_code(iso2_list[i])

I feel like all the syntaxs I know thinly have collapsed in the way of using for loop, def, and if phrases, so I'd appreciate it if you could point out how to modify them and which parts I don't know well.

python for loops def

2022-09-20 13:33

2 Answers

When you get confused while coding, it is helpful to code in Korean sometimes.

Def country name as code (name you want to know):
    If the country you want to know is not None:
        for country name, code in name_code_dict:
            If country name === country you want to know:
                return code
    return None

Def country to continent (name of country you want to know):
    Code you want to know = the name of the country as a code (the name of the country you want to know)
    If the code you want to know is not None:
        for code, continent in code_continent_dict:
            If code === the code you want to know:
                the return continent
    return None

You see what's going on?
I believe this would have been helpful, so I'll cut it down.

+ Just in case you're curious about where to get code_content_dict... It's libraries like pycountry that provide it.


2022-09-20 13:33

This is a method of converting by adding columns one by one to the pandas data frame.


>>> import pandas as pd
>>> import pycountry_convert as pc

>>> country_names = [ "Japan", "China", "Germany" ]
>>> for n in country_names:
    print(n, pc.country_name_to_country_alpha2(n))


Japan JP
China CN
Germany DE
>>> pc.country_alpha2_to_country_name("KR")
'Korea, Republic of'
>>> pc.country_alpha2_to_country_name("KP")
"Korea, Democratic People's Republic of"

>>> country_names = [ "Korea, Republic of", "Japan", "Germany" ]
>>> country_alpha2 = [ pc.country_name_to_country_alpha2(name) for name in country_names ]
>>> country_alpha2
['KR', 'JP', 'DE']
>>> 
>>> 
>>> df_countries = pd.DataFrame( { "name": country_names } )
>>> df_countries
                 name
0  Korea, Republic of
1               Japan
2             Germany
>>> df_countries["alpha2"] = df_countries["name"].apply(pc.country_name_to_country_alpha2)
>>> df_countries
                 name alpha2
0  Korea, Republic of     KR
1               Japan     JP
2             Germany     DE
>>> df_countries["continent_code"] = df_countries["alpha2"].apply(pc.country_alpha2_to_continent_code)
>>> df_countries
                 name alpha2 continent_code
0  Korea, Republic of     KR             AS
1               Japan     JP             AS
2             Germany     DE             EU

>>> df_countries["continent"] = df_countries["continent_code"].apply(pc.convert_continent_code_to_continent_name)
>>> df_countries
                 name alpha2 continent_code continent
0  Korea, Republic of     KR             AS      Asia
1               Japan     JP             AS      Asia
2             Germany     DE             EU    Europe
>>> 


2022-09-20 13:33

If you have any answers or tips


© 2022 pinfo. All rights reserved.