Skip to content

Instantly share code, notes, and snippets.

@ThiagoFPMR
Created November 27, 2020 03:11
Show Gist options
  • Save ThiagoFPMR/c9b3e30767e4ce7cb66bb28524d09811 to your computer and use it in GitHub Desktop.
Save ThiagoFPMR/c9b3e30767e4ce7cb66bb28524d09811 to your computer and use it in GitHub Desktop.
# Defining the functio we'll use to convert the columns to snakecase
def to_snakecase (cols):
map_dict = {}
for col in cols:
map_dict[col] = col.lower().strip().replace(' ', '_')
return map_dict
# Defining the function we'll use to change the country names to the same format
def normalize_country (data):
if pc.countries.get(official_name=data):
return pc.countries.get(official_name=data).name
elif pc.countries.get(name=data):
return pc.countries.get(name=data).name
elif pc.countries.get(alpha_3=data):
return pc.countries.get(alpha_3=data).name
elif pc.countries.get(alpha_2=data):
return pc.countries.get(alpha_2=data).name
# Applying both functions to the COVID-19 Dataset
covid_df.rename(to_snakecase(covid_df.columns), axis=1, inplace=True)
covid_df = covid_df[covid_df.date == '2020-10-12'] # Dropping instances from previous dates
covid_df.drop('date', axis=1, inplace=True)
covid_df.country = covid_df.country.apply(normalize_country)
# Applying both functions to the Human Capital Index Dataset
hci_df.rename(to_snakecase(hci_df.columns), axis=1, inplace=True)
hci_df.rename({'country_name':'country'}, axis=1, inplace=True)
hci_df.country = hci_df.country.apply(normalize_country)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment