Skip to content

Instantly share code, notes, and snippets.

@maclandrol
Last active April 15, 2018 21:32
Show Gist options
  • Save maclandrol/5ec254ef3d52775f7baf8ef06e214017 to your computer and use it in GitHub Desktop.
Save maclandrol/5ec254ef3d52775f7baf8ef06e214017 to your computer and use it in GitHub Desktop.
Origin of imported Good in Benin (2016)
from pycountry_convert import country_alpha2_to_continent_code as a2ctc
from pycountry_convert import convert_continent_code_to_continent_name as ctc2ctn
from pycountry_convert import country_alpha3_to_country_alpha2 as a3a2
from pycountry_convert import country_alpha2_to_country_name as a2cn
from functools import partial
from d3IpyPlus import *
# let's keep the following columns only
datafile = "en_profile_country_ben_import_des.csv"
cols = ['year', 'country_origin_id', 'country_destination_id', 'import_val']
# then process the data to add new columns corresponding to
# each country full name and continent
def add_continent(df, colname):
dest_info = []
for x in df[colname]:
a2code = a3a2(x)
country = x
contnt = "Unknown" #because incf doesn't have all countries
try:
if a2code:
country = a2cn(a2code)
contnt = ctc2ctn(a2ctc(a2code))
except:
pass
dest_info.append([country, contnt])
dest_info = pd.DataFrame(dest_info, columns=['country_destination_name', 'country_destination_continent'])
df = pd.concat([df, dest_info], axis=1)
return df
# we are going to pass add_continent as an argument for from_csv
df = from_csv(datafile, columns=cols,
process_func=partial(add_continent, colname='country_destination_id'))
print(df.head())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment