🌐
PyPI
pypi.org › project › pycountry-convert
pycountry-convert · PyPI
Extension of Python package pycountry providing conversion functions.
      » pip install pycountry-convert
    
Published   Feb 18, 2018
Version   0.7.2
🌐
GitHub
github.com › jefftune › pycountry-convert
GitHub - jefftune/pycountry-convert: Python conversion functions between ISO country codes, countries, and continents. · GitHub
Python conversion functions between ISO country codes, countries, and continents. - jefftune/pycountry-convert
Starred by 22 users
Forked by 28 users
Languages   Python 82.0% | Makefile 18.0%
Discussions

The DRC in Pycountry
I'm not sure what you mean by "ISO-3". If you mean ISO 3166-1 alpha-3, then as shown here it is COD. More on reddit.com
🌐 r/learnpython
5
2
December 29, 2024
python - Basic function to convert Country name to ISO code using Pycountry - Code Review Stack Exchange
I have written a basic function which converts country names into country codes using pycountry library. More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
March 11, 2020
python - I cannot get pycountry_convert to work in my code - Stack Overflow
I came across this example to convert country to continent I could not find the package pycountry_convert so I installed pycontry and pycountry-convert but using both I cannot run the following code More on stackoverflow.com
🌐 stackoverflow.com
python - How to use pycountry_convert to create a new column in a DataFrame that lists the continent that the country is in? - Stack Overflow
I also know of pycountry_convert, that outputs a continent code. More on stackoverflow.com
🌐 stackoverflow.com
April 7, 2022
🌐
Stack Overflow
stackoverflow.com › questions › 71479584 › how-to-convert-a-list-of-countries-into-their-corresponding-continents-using-pyc
python - How to convert a list of countries into their corresponding continents using pycountry_convert? - Stack Overflow
I am using this code for help link: country convert to continent · but i got error when try to execute the code given above : " KeyError: "Invalid Country Alpha-2 code: 'AQ'" " ... Copyimport 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 countries = list(temp['Area']) [country_to_continent(country)for country in countries]
🌐
Medium
medium.com › @HeCanThink › pycountry-your-passport-to-iso-country-codes-names-and-more-2c39ef6668c0
PyCountry: Your Passport to ISO Country Codes, Names and More 🛂 | by Manoj Das | Medium
August 11, 2023 - PyCountry: Your Passport to ISO Country Codes, Names and More 🛂 What is pycountry? Access ISO databases for the standards Languages, Countries, Deleted countries, Subdivisions of countries …
🌐
Anaconda.org
anaconda.org › conda-forge › pycountry
pycountry - conda-forge | Anaconda.org
conda-forge/pycountry · Community · pycountry provides the ISO databases for the standards · Copied fromcf-staging / pycountry · Overview · Files 7 · Labels 4 · Badges · Versions · 24.6.1 · To install this package, run one of the following: $conda install conda-forge::pycountry ·
🌐
PyPI
pypi.org › project › country-converter
country-converter · PyPI
The package pycountry provides access to the official ISO databases for historic countries, country subdivisions, languages and currencies. In case you need to convert non-English country names, countrynames includes an extensive database of country names in different languages and functions to convert them to the different ISO 3166 standards.
      » pip install country-converter
    
Published   Oct 22, 2025
Version   1.3.2
🌐
GitHub
github.com › threefoldtecharchive › sandbox_threebot_linux64 › blob › master › usr › local › lib › python3.6 › dist-packages › pycountry_convert › convert_countries.py
sandbox_threebot_linux64/usr/local/lib/python3.6/dist-packages/pycountry_convert/convert_countries.py at master · threefoldtecharchive/sandbox_threebot_linux64
October 12, 2023 - prebuilt sandbox of threebot for quick-start development - sandbox_threebot_linux64/usr/local/lib/python3.6/dist-packages/pycountry_convert/convert_countries.py at master · threefoldtecharchive/sandbox_threebot_linux64
Author   threefoldtecharchive
🌐
Parseltongue
parseltongue.co.in › exploring-countries-with-the-pycountry-module-in-python
ParselTongue - Exploring Countries with the pycountry Module in Python
September 1, 2023 - import pycountry country_name = "India" country = pycountry.countries.get(name=country_name) print(country)
Find elsewhere
🌐
Kaggle
kaggle.com › questions-and-answers › 513313
Converting country code to its corresponding full name- help | Kaggle
Hello @igroy6, if the module you want to work with was not found, then you probably have to install it first. You can do so by using the command !pip install pycountry-convert in your first code cell.
🌐
Reddit
reddit.com › r/learnpython › the drc in pycountry
r/learnpython on Reddit: The DRC in Pycountry
December 29, 2024 -

Hey, I am trying to change country names to ones that are recognised by pycountry so that I can get their ISO-3 codes, does anyone know what it is for the DRC (Democratic Republic of the Congo)?

Wikipedia says: 'Congo, Democratic Republic of the' but it is not working for me

Edit: its 'Congo, The Democratic Republic of the' if anyone cares

🌐
Auth0
auth0.com › home › blog › how to read and remove metadata from your photos with python
How to Read, Edit, and Erase Location and other EXIF Metadata from Your Photos With Python
January 12, 2024 - The code uses reversegeocoder_’s search() method to convert each photo’s decimal latitude and longitude into a collection of the following information: ... It then uses pycountry’s get() method to convert the country code provided by reverse_geocoder into a tuple containing the corresponding common and official country names.
🌐
Debian
packages.debian.org › bookworm › python3-translate
I Challenge Thee
I Challenge Thee · AI scrapers break the web, to use this page you'll need JavaScript enabled
Top answer
1 of 4
13

I always get my code to be as clean as possible before starting work on performance. Here you have a bare except, which can hide errors. Change it to something better, for now I'll change it to Exception. You are also hiding a potential IndexError and AttributeError if the data ever changes shape as they are in the try.

def do_fuzzy_search(country):
    try:
        result = pycountry.countries.search_fuzzy(country)
    except Exception:
        return np.nan
    else:
        return result[0].alpha_3

Since there are roughly 200 sovereign states and you're working with 17003 rows you likely have a lot of the same values hitting a costly function. To resolve this issue you can use functools.lru_cache. Running in amortized \$O(n)\$ time and \$O(n)\$ space.

@functools.lru_cache(None)
def do_fuzzy_search(country):
    ...

Alternately you can sort the data by the provided countries name and then get each country once. Running in \$O(n\log n)\$ time and \$O(1)\$ space.

2 of 4
7

Apart from speeding up your code, there are some other improvements possible that I don't see mentioned yet.

The speedup

As other users have already picked up, you only need to transform around 200 countries instead of 17k rows. You can do this efficiently by storing the mapping in a dictionary:

iso_map = {country: do_fuzzy_search(country) for country in df["Country"].unique()}
df["country_code"] = df["Country"].map(iso_map)

This is in essence very similar to using the lru_cache proposed by Peilonrayz, but it's a bit more explicit.


Consistent naming

As a side note, I would advise you to use a consistent naming scheme in your DataFrame. This doesn't have to follow the python convention, but it should be easy to remember.

For example, turn all columns to lowercase by using something like:

df = df.rename(columns = {name: name.lower() for name in df.columns}

Using categoricals

Since the number of countries (and isocodes) is limited, you might want to consider using categorical columns. These have the advantage of requiring less memory and being faster on some operations.

It might not be worth the extra lines of code in your situation, but it's something useful to keep in the back of your mind.

🌐
Superblocks
docs-legacy.superblocks.com › python
Python | Superblocks Docs
Write Python code in your Superblocks backend APIs to filter, transform, and enrich any type of business data pulled from databases or entered by users.
🌐
Stack Overflow
stackoverflow.com › questions › 70181879 › i-cannot-get-pycountry-convert-to-work-in-my-code
python - I cannot get pycountry_convert to work in my code - Stack Overflow
Copyimport pycountry_convert as pc # Example: Convert country code 'US' to continent code country_code = 'US' continent_code = pc.country_alpha2_to_continent_code(country_code) print(f"Country Code: {country_code} -> Continent Code: {continent_code}")
🌐
Cloudsmith
cloudsmith.com › navigator › pypi › pycountry-convert
pycountry-convert (0.7.2) - pypi Package Quality | Cloudsmith Navigator
Learn all about the quality, security, and current maintenance status of pycountry-convert using Cloudsmith Navigator
🌐
Kaggle
kaggle.com › code › qlasty › localization-context-country-continent
Localization context : country/continent
November 22, 2018 - Explore and run AI code with Kaggle Notebooks | Using data from Quick, Draw! Doodle Recognition Challenge