Skip to main content
1 of 4

Basic function to convert Country name to ISO code using Pycountry

I am working on a dataset which contains a column with common country names.

Task: To convert country name into standard ISO names

I have written a basic function which converts country names into country codes using pycountry library.But I am facing issues when my dataset contains large number of rows.Its too slow.

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

Firstly,Suggestions on the code to improve its performance would be welcome.

Secondly,I would like to know in case there is another faster way to do this.