Get continent name from country using pycountry
Asked Answered
C

3

19

How to convert continent name from country name using pycountry. I have a list of country like this

country = ['India', 'Australia', ....]

And I want to get continent name from it like.

continent = ['Asia', 'Australia', ....]

Copywriter answered 29/4, 2019 at 19:58 Comment(3)
I would suggest to use pypi.org/project/pycountry-convert.Friedman
@VikasYadav Thanks for suggestion! So according to this I should country name to ISO_2 and from that to continent name?Copywriter
for country in pycountry.countries: countries[country.name] = country.alpha_2 This will convert country to alpha_2, Is there any way like this which can convert country name to continent name?Copywriter
S
10

Looking at the documentation, would something like this do the trick?:

country_alpha2_to_continent_code()

It converts country code(eg: NO, SE, ES) to continent name.

If first you need to acquire the country code you could use:

country_name_to_country_alpha2(cn_name, cn_name_format="default")

to get the country code from country name.

Full example:

import pycountry_convert as pc

country_code = pc.country_name_to_country_alpha2("China", cn_name_format="default")
print(country_code)
continent_name = pc.country_alpha2_to_continent_code(country_code)
print(continent_name)
Subordinary answered 29/4, 2019 at 20:10 Comment(0)
L
15

All the tools you need are provided in pycountry-convert.

Here is an example of how you can create your own function to make a direct conversion from country to continent name, using pycountry's tools:

import pycountry_convert as pc

def country_to_continent(country_name):
    country_alpha2 = pc.country_name_to_country_alpha2(country_name)
    country_continent_code = pc.country_alpha2_to_continent_code(country_alpha2)
    country_continent_name = pc.convert_continent_code_to_continent_name(country_continent_code)
    return country_continent_name

# Example
country_name = 'Germany'
print(country_to_continent(country_name))

Out[1]: Europe 

Hope it helps!

Remember you can access function descriptions using '?? function_name'

?? pc.country_name_to_country_alpha2
Lattie answered 3/12, 2019 at 22:9 Comment(2)
Sometimes pycountry_convert doesn't work; for example, for country_name in {'Antarctica', 'Western Sahara', 'French Southern Territories', 'Sint Maarten (Dutch part)', 'Pitcairn', ...} it doesn't recognize the associated alpha2 code.Anacardiaceous
User asked about pycountryHoratio
S
10

Looking at the documentation, would something like this do the trick?:

country_alpha2_to_continent_code()

It converts country code(eg: NO, SE, ES) to continent name.

If first you need to acquire the country code you could use:

country_name_to_country_alpha2(cn_name, cn_name_format="default")

to get the country code from country name.

Full example:

import pycountry_convert as pc

country_code = pc.country_name_to_country_alpha2("China", cn_name_format="default")
print(country_code)
continent_name = pc.country_alpha2_to_continent_code(country_code)
print(continent_name)
Subordinary answered 29/4, 2019 at 20:10 Comment(0)
K
7
from pycountry_convert import country_alpha2_to_continent_code, country_name_to_country_alpha2

continents = {
    'NA': 'North America',
    'SA': 'South America', 
    'AS': 'Asia',
    'OC': 'Australia',
    'AF': 'Africa',
    'EU': 'Europe'
}

countries = ['India', 'Australia']

[continents[country_alpha2_to_continent_code(country_name_to_country_alpha2(country))] for country in countries]

Don't know what to do with Antarctica continent ¯_(ツ)_/¯

Kelwin answered 29/4, 2019 at 20:14 Comment(2)
@RomanAlexandrovich this mapping already exists: pycountry_converter already has convert_continent_code_to_continent_name method, you do not need to convert it yourself.Lovieloving
The code segment worked great and solve my problem. Thank you. I would ask for us to not fall into colonialist mentalities: OC == Oceania, as opposed to "Australia"Permute

© 2022 - 2024 — McMap. All rights reserved.