14

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

VIBHU BAROT
  • 317
  • 2
  • 4
  • 13
  • I would suggest to use https://pypi.org/project/pycountry-convert/. – Vikas Yadav Apr 29 '19 at 20:01
  • @VikasYadav Thanks for suggestion! So according to this I should country name to ISO_2 and from that to continent name? – VIBHU BAROT Apr 29 '19 at 20:05
  • 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? – VIBHU BAROT Apr 29 '19 at 20:07

3 Answers3

11

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
Isra Mata
  • 111
  • 1
  • 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. – Francesco Pinna Nov 10 '22 at 08:30
9

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)
Oliver H. D.
  • 315
  • 1
  • 6
8
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 ¯_(ツ)_/¯

drdebmath
  • 58
  • 6
  • @RomanAlexandrovich this mapping already exists: pycountry_converter already has `convert_continent_code_to_continent_name` method, you do not need to convert it yourself. – Eduardo Oct 23 '21 at 13:04
  • 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" – ExoWanderer Nov 25 '22 at 15:01