I have an array of countries. I would like to run this array through a function and append the output of the function as a column to a dataframe.
I used the apply
method but keep getting a KeyError
. I am not sure what I am doing wrong.
Code
import matplotlib.pyplot as plt
import pandas as pd
import pycountry_convert as pc
data - pd.read_csv('/content/2019.csv', index_col=0)
data.loc[71, 'Country or region'] = 'Trinidad and Tobago'
country_region = data['Country or region']
for country in country_region:
country_code = pc.country_name_to_country_alpha2(country)
data['Continent'].apply(pc.country_alpha2_to_continent_code(country_code))
Here is a screenshot of my error, for more details.