In [37]:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import scipy as sp
import seaborn as sns
%matplotlib inline
sns.set_context("notebook")
sns.set(style="whitegrid")

# 2014 expenditure the US spent on foreign aid. 
# Found here: https://explorer.usaid.gov/query
long_aid = pd.read_excel('data/2000_2014_foreign_aid.xlsx')
aid = long_aid[long_aid.fiscal_year == 2014]

for attribute in aid:
    print attribute


country_code
country_name
region_name
income_group_name
implementing_agency_name
channel_category_name
dac_category_name
dac_sector_name
dac_purpose_name
funding_agency_name
assistance_category_name
activity_name
activity_start_date
activity_end_date
transaction_type_name
fiscal_year
current_amount
constant_amount
numeric_year

In [38]:
# Let's look at the development assistance committee categories
categories = aid['dac_category_name'].unique()

In [39]:
def total_costs(dataframe, region_label, cost_label):
    costs = {}
    for index,row in dataframe.iterrows():
        region = row[region_label]
        cost = row[cost_label]
        if cost >= 0:
            if region in costs:
                costs[region] += cost
            else:
                costs[region] = cost
    return costs

def costs_of(df, category, region_label, cost_label):
    aid_category = df[df.dac_category_name == category]
    return total_costs(aid_category, region_label, cost_label)

def dict_to_df(dictionary, cols):
    return pd.DataFrame(dictionary.items(), columns=cols)

totals = total_costs(aid, 'dac_category_name', 'constant_amount')
totals_df = dict_to_df(totals, ['Category', 'Cost'])
totals_df


Out[39]:
Category Cost
0 Economic Growth 1.475500e+09
1 Commodity Assistance 7.191740e+08
2 Infrastructure 1.685262e+09
3 Health and Population 1.940597e+10
4 Humanitarian 1.190496e+10
5 Governance 6.790337e+09
6 Other 1.190465e+10
7 Education 2.090078e+09
8 Agriculture 2.535445e+09

In [40]:
totals_df.to_csv('data/totals.csv', index=False)

In [41]:
years = range(2000,2015)

year_totals = {}
for year in years:
    year_totals[year] = total_costs(long_aid[long_aid.fiscal_year == year], 'dac_category_name', 'constant_amount')

In [42]:
year_totals_df = pd.DataFrame.from_dict(year_totals)

year_totals_dfs = {}
for year in year_totals:
    year_totals_dfs[year] = dict_to_df(year_totals[year], ['Expenditure', 'Cost'])
    
year_totals_df = pd.concat(year_totals_dfs)
year_totals_df['Year'] = year_totals_df.index.get_level_values(0)
year_totals_df.to_csv('data/year_totals.csv', index=False)
print(year_totals_df.columns)


Index([u'Expenditure', u'Cost', u'Year'], dtype='object')

In [43]:
country_costs = {}
for category in categories:
    country_costs[category] = costs_of(aid, category, 'country_name', 'constant_amount')
    
country_costs_df = pd.DataFrame.from_dict(country_costs)
country_costs_df.index.name = 'Country'
country_costs_df = country_costs_df.reset_index(level=0)

regions = {}
for index,row in aid.iterrows():
    region = row['region_name']
    country = row['country_name']
    regions[country] = region

country_costs_df['Region'] = country_costs_df['Country']
country_costs_df['Region'] = country_costs_df['Region'].replace(regions)
country_costs_df


Out[43]:
Country Agriculture Commodity Assistance Economic Growth Education Governance Health and Population Humanitarian Infrastructure Other Region
0 Afghanistan 1.976950e+08 8.638306e+05 1.849758e+08 2.871322e+08 1.266806e+09 1.986964e+08 3.885368e+08 4.677469e+08 3.471415e+08 South and Central Asia
1 Albania NaN NaN 5.657506e+06 1.003898e+06 2.183025e+07 1.109404e+06 NaN 1.034075e+06 9.384768e+06 Europe and Eurasia
2 Algeria NaN NaN NaN NaN 2.031292e+05 NaN 1.370466e+07 4.926259e+05 NaN Middle East and North Africa
3 Angola 1.836293e+03 NaN 1.267785e+06 9.612906e+05 2.598076e+06 8.504145e+07 9.681239e+06 4.123279e+05 1.226135e+07 Sub-Saharan Africa
4 Argentina 3.386606e+05 NaN 6.248782e+05 NaN 9.359892e+03 6.082255e+04 NaN NaN 4.602545e+05 Western Hemisphere
5 Armenia NaN 1.476902e+05 1.604555e+07 2.717406e+06 3.018401e+07 6.909769e+06 8.172420e+05 1.286178e+05 1.032673e+07 Europe and Eurasia
6 Asia Region 1.730555e+06 9.410217e+05 3.813249e+06 1.472429e+07 3.611652e+06 5.962107e+07 4.781824e+05 9.588318e+05 3.965617e+08 World
7 Asia, Middle East and North Africa Region NaN NaN NaN NaN NaN NaN NaN 1.520396e+04 NaN World
8 Azerbaijan 3.694842e+03 NaN 5.561683e+06 2.807921e+06 1.557551e+07 2.031289e+05 3.910863e+05 5.837923e+05 7.879799e+06 Europe and Eurasia
9 Bahrain NaN NaN NaN NaN 9.476477e+05 NaN NaN NaN NaN Middle East and North Africa
10 Bangladesh 7.924956e+07 5.488573e+07 3.848350e+06 1.081010e+07 3.388031e+07 1.875124e+08 3.473604e+07 4.202217e+03 6.458002e+07 South and Central Asia
11 Barbados NaN NaN NaN NaN 4.769326e+04 NaN NaN NaN 8.779776e+03 Western Hemisphere
12 Belarus NaN NaN 3.859201e+06 1.021747e+06 1.388936e+07 2.463130e+05 NaN NaN 1.723356e+06 Europe and Eurasia
13 Belize 1.379057e+04 NaN NaN 1.633410e+05 9.263452e+04 1.929162e+06 NaN NaN 3.323708e+05 Western Hemisphere
14 Benin 1.689822e+06 NaN 1.091354e+06 4.421394e+06 5.047726e+06 3.900945e+07 8.130298e+03 3.909245e+06 1.652693e+07 Sub-Saharan Africa
15 Bhutan NaN NaN NaN NaN NaN NaN 3.716537e+05 NaN NaN South and Central Asia
16 Bolivia 2.907104e+05 8.882587e+03 4.827251e+05 7.251453e+04 2.965588e+05 2.444699e+06 NaN NaN 3.024652e+06 Western Hemisphere
17 Bosnia and Herzegovina 1.881268e+06 NaN 2.434573e+07 NaN 4.566401e+07 NaN 5.344932e+06 NaN 1.410759e+07 Europe and Eurasia
18 Botswana NaN NaN 8.759460e+04 6.623375e+03 4.355922e+05 1.015737e+08 4.926259e+04 NaN 5.970238e+05 Sub-Saharan Africa
19 Brazil 3.324252e+06 NaN 6.464437e+05 3.724833e+05 4.277372e+05 4.711948e+06 5.938464e+04 6.258904e+06 1.524227e+07 Western Hemisphere
20 Brunei NaN NaN NaN NaN NaN NaN 1.477878e+05 NaN NaN East Asia and Oceania
21 Bulgaria NaN NaN NaN 9.562666e+05 4.982361e+04 NaN 4.926259e+04 NaN 1.208136e+06 Europe and Eurasia
22 Burkina Faso 7.409563e+07 3.137842e+07 1.388834e+05 2.809229e+06 2.847268e+07 1.776089e+07 1.814744e+07 1.005767e+08 1.824581e+07 Sub-Saharan Africa
23 Burma (Myanmar) 8.655740e+06 NaN 3.018086e+06 6.630178e+06 5.145028e+07 2.224514e+07 1.006458e+08 1.614724e+06 1.589971e+07 East Asia and Oceania
24 Burundi 1.117907e+06 1.040613e+06 3.377622e+05 NaN 6.288868e+06 3.230932e+07 2.449248e+07 NaN 3.326015e+06 Sub-Saharan Africa
25 Cabo Verde 2.785901e+05 NaN 3.574923e+05 NaN 1.061337e+05 3.088049e+06 NaN NaN 7.481091e+06 Sub-Saharan Africa
26 Cambodia 2.194463e+07 NaN 2.404897e+06 2.520314e+06 2.538842e+07 9.619936e+07 1.236997e+06 NaN 3.987167e+07 East Asia and Oceania
27 Cameroon NaN 2.807968e+05 3.029649e+05 3.676692e+06 2.027834e+06 5.544217e+07 3.013918e+07 NaN 2.586790e+06 Sub-Saharan Africa
28 Canada NaN NaN 3.944426e+03 NaN NaN NaN NaN NaN 4.740047e+05 Western Hemisphere
29 Caribbean Region NaN 1.232950e+06 6.334080e+06 4.171755e+06 8.930512e+06 3.386467e+07 3.259057e+06 NaN 1.487713e+07 Western Hemisphere
... ... ... ... ... ... ... ... ... ... ... ...
153 Sudan NaN 1.623290e+06 NaN NaN 1.320390e+07 1.728187e+06 3.781959e+08 NaN 1.068070e+07 Sub-Saharan Africa
154 Sudan (former) 9.139544e+05 3.172975e+04 1.996454e+06 2.544591e+05 3.781330e+06 8.763822e+06 2.407008e+08 3.791914e+06 1.572475e+07 Sub-Saharan Africa
155 Suriname NaN NaN NaN NaN 3.157338e+02 NaN NaN NaN 4.232622e+03 Western Hemisphere
156 Swaziland NaN NaN NaN 2.292054e+06 1.441139e+05 8.276367e+07 3.019916e+06 NaN 1.206727e+06 Sub-Saharan Africa
157 Switzerland NaN NaN NaN NaN NaN 1.416848e+06 NaN NaN 3.941007e+05 Europe and Eurasia
158 Syria NaN 2.382808e+05 NaN NaN 9.284948e+07 NaN 1.458470e+09 NaN 6.794437e+06 Middle East and North Africa
159 Tajikistan 1.871526e+07 NaN 3.248985e+06 5.882361e+06 2.449764e+07 1.736964e+07 9.852518e+04 1.235627e+06 4.290652e+06 South and Central Asia
160 Tanzania 8.379234e+07 1.435167e+07 4.715899e+07 3.273734e+07 1.325743e+07 8.039658e+08 9.781955e+06 9.545648e+07 6.547476e+07 Sub-Saharan Africa
161 Thailand NaN NaN NaN 5.734602e+06 1.810380e+07 1.988337e+07 6.296907e+07 6.601187e+03 1.417195e+06 East Asia and Oceania
162 Timor-Leste 3.667280e+06 2.375746e+07 2.184950e+06 2.981390e+06 1.545731e+07 6.600830e+06 3.507495e+06 NaN 1.221403e+07 East Asia and Oceania
163 Togo NaN NaN NaN 1.620912e+06 3.034772e+05 2.591279e+06 NaN NaN 1.870264e+06 Sub-Saharan Africa
164 Tonga NaN NaN NaN 1.731654e+06 NaN NaN 9.297743e+05 NaN NaN East Asia and Oceania
165 Trinidad and Tobago NaN NaN NaN NaN NaN NaN NaN NaN 4.926259e+04 Western Hemisphere
166 Tunisia NaN NaN 6.261098e+06 NaN 3.562968e+07 5.367121e+04 NaN NaN 2.004930e+06 Middle East and North Africa
167 Turkey NaN 1.722756e+05 6.653816e+04 2.475169e+05 4.510667e+05 3.479333e+05 1.603414e+08 2.399003e+06 7.464268e+05 Europe and Eurasia
168 Turkmenistan 5.933679e+02 NaN 4.019272e+06 2.230945e+05 3.642282e+06 1.526698e+06 NaN 9.605331e+05 2.004305e+06 South and Central Asia
169 Uganda 7.860588e+07 1.700393e+07 6.610287e+06 3.485640e+07 1.866597e+07 6.723724e+08 9.516663e+07 1.132422e+07 5.594607e+07 Sub-Saharan Africa
170 Ukraine 3.431983e+06 NaN 9.266748e+06 5.071819e+06 1.094211e+08 4.697222e+07 1.275243e+07 4.642676e+06 5.030748e+07 Europe and Eurasia
171 Uruguay NaN NaN 1.880464e+05 4.700834e+04 1.976440e+04 NaN NaN NaN 8.026157e+04 Western Hemisphere
172 Uzbekistan 6.439760e+05 3.670063e+05 6.139935e+06 1.093630e+05 5.762711e+06 7.145383e+06 NaN NaN 2.474078e+06 South and Central Asia
173 Vanuatu NaN NaN NaN 2.681191e+06 6.253620e+03 1.757286e+06 1.143126e+06 NaN 1.846901e+05 East Asia and Oceania
174 Venezuela NaN NaN NaN NaN 5.583195e+06 NaN 2.098542e+06 NaN 1.561172e+06 Western Hemisphere
175 Vietnam NaN NaN 9.169212e+06 3.312014e+06 1.329477e+07 8.353624e+07 3.029380e+06 3.773216e+06 6.874704e+07 East Asia and Oceania
176 West Africa Region 3.680867e+07 1.990721e+06 1.287467e+07 1.081367e+06 5.067408e+07 4.694186e+07 2.466948e+08 3.323716e+06 3.651115e+07 Sub-Saharan Africa
177 West Bank/Gaza 5.489504e+06 NaN 2.206180e+07 2.363023e+07 2.421555e+08 5.887739e+07 3.559090e+08 4.807701e+07 5.738705e+07 Middle East and North Africa
178 Western Hemisphere Region 3.334605e+05 NaN 1.283435e+06 NaN NaN NaN NaN NaN 1.042143e+06 Western Hemisphere
179 World 5.366879e+08 1.018187e+08 1.519282e+08 2.780233e+08 9.175380e+08 7.944769e+09 1.054667e+09 2.033002e+07 6.962158e+09 World
180 Yemen 1.627746e+07 5.062204e+05 3.035532e+06 2.346827e+07 6.556413e+07 1.523047e+07 2.250901e+08 1.436602e+05 1.796912e+07 Middle East and North Africa
181 Zambia 2.889999e+07 NaN 3.574699e+06 2.694545e+07 7.785643e+06 8.484630e+08 4.762765e+06 3.694694e+04 7.803364e+07 Sub-Saharan Africa
182 Zimbabwe 8.189685e+06 3.330206e+07 8.155591e+06 9.609358e+04 2.873430e+07 1.976670e+08 4.380501e+07 NaN 2.654714e+07 Sub-Saharan Africa

183 rows × 11 columns


In [44]:
country_costs_df.columns = ['2014 ' + col for col in country_costs_df.columns]
country_costs_df = country_costs_df.rename(columns={'2014 Country': 'Country', '2014 Region': 'Region'})

country_costs_df


Out[44]:
Country 2014 Agriculture 2014 Commodity Assistance 2014 Economic Growth 2014 Education 2014 Governance 2014 Health and Population 2014 Humanitarian 2014 Infrastructure 2014 Other Region
0 Afghanistan 1.976950e+08 8.638306e+05 1.849758e+08 2.871322e+08 1.266806e+09 1.986964e+08 3.885368e+08 4.677469e+08 3.471415e+08 South and Central Asia
1 Albania NaN NaN 5.657506e+06 1.003898e+06 2.183025e+07 1.109404e+06 NaN 1.034075e+06 9.384768e+06 Europe and Eurasia
2 Algeria NaN NaN NaN NaN 2.031292e+05 NaN 1.370466e+07 4.926259e+05 NaN Middle East and North Africa
3 Angola 1.836293e+03 NaN 1.267785e+06 9.612906e+05 2.598076e+06 8.504145e+07 9.681239e+06 4.123279e+05 1.226135e+07 Sub-Saharan Africa
4 Argentina 3.386606e+05 NaN 6.248782e+05 NaN 9.359892e+03 6.082255e+04 NaN NaN 4.602545e+05 Western Hemisphere
5 Armenia NaN 1.476902e+05 1.604555e+07 2.717406e+06 3.018401e+07 6.909769e+06 8.172420e+05 1.286178e+05 1.032673e+07 Europe and Eurasia
6 Asia Region 1.730555e+06 9.410217e+05 3.813249e+06 1.472429e+07 3.611652e+06 5.962107e+07 4.781824e+05 9.588318e+05 3.965617e+08 World
7 Asia, Middle East and North Africa Region NaN NaN NaN NaN NaN NaN NaN 1.520396e+04 NaN World
8 Azerbaijan 3.694842e+03 NaN 5.561683e+06 2.807921e+06 1.557551e+07 2.031289e+05 3.910863e+05 5.837923e+05 7.879799e+06 Europe and Eurasia
9 Bahrain NaN NaN NaN NaN 9.476477e+05 NaN NaN NaN NaN Middle East and North Africa
10 Bangladesh 7.924956e+07 5.488573e+07 3.848350e+06 1.081010e+07 3.388031e+07 1.875124e+08 3.473604e+07 4.202217e+03 6.458002e+07 South and Central Asia
11 Barbados NaN NaN NaN NaN 4.769326e+04 NaN NaN NaN 8.779776e+03 Western Hemisphere
12 Belarus NaN NaN 3.859201e+06 1.021747e+06 1.388936e+07 2.463130e+05 NaN NaN 1.723356e+06 Europe and Eurasia
13 Belize 1.379057e+04 NaN NaN 1.633410e+05 9.263452e+04 1.929162e+06 NaN NaN 3.323708e+05 Western Hemisphere
14 Benin 1.689822e+06 NaN 1.091354e+06 4.421394e+06 5.047726e+06 3.900945e+07 8.130298e+03 3.909245e+06 1.652693e+07 Sub-Saharan Africa
15 Bhutan NaN NaN NaN NaN NaN NaN 3.716537e+05 NaN NaN South and Central Asia
16 Bolivia 2.907104e+05 8.882587e+03 4.827251e+05 7.251453e+04 2.965588e+05 2.444699e+06 NaN NaN 3.024652e+06 Western Hemisphere
17 Bosnia and Herzegovina 1.881268e+06 NaN 2.434573e+07 NaN 4.566401e+07 NaN 5.344932e+06 NaN 1.410759e+07 Europe and Eurasia
18 Botswana NaN NaN 8.759460e+04 6.623375e+03 4.355922e+05 1.015737e+08 4.926259e+04 NaN 5.970238e+05 Sub-Saharan Africa
19 Brazil 3.324252e+06 NaN 6.464437e+05 3.724833e+05 4.277372e+05 4.711948e+06 5.938464e+04 6.258904e+06 1.524227e+07 Western Hemisphere
20 Brunei NaN NaN NaN NaN NaN NaN 1.477878e+05 NaN NaN East Asia and Oceania
21 Bulgaria NaN NaN NaN 9.562666e+05 4.982361e+04 NaN 4.926259e+04 NaN 1.208136e+06 Europe and Eurasia
22 Burkina Faso 7.409563e+07 3.137842e+07 1.388834e+05 2.809229e+06 2.847268e+07 1.776089e+07 1.814744e+07 1.005767e+08 1.824581e+07 Sub-Saharan Africa
23 Burma (Myanmar) 8.655740e+06 NaN 3.018086e+06 6.630178e+06 5.145028e+07 2.224514e+07 1.006458e+08 1.614724e+06 1.589971e+07 East Asia and Oceania
24 Burundi 1.117907e+06 1.040613e+06 3.377622e+05 NaN 6.288868e+06 3.230932e+07 2.449248e+07 NaN 3.326015e+06 Sub-Saharan Africa
25 Cabo Verde 2.785901e+05 NaN 3.574923e+05 NaN 1.061337e+05 3.088049e+06 NaN NaN 7.481091e+06 Sub-Saharan Africa
26 Cambodia 2.194463e+07 NaN 2.404897e+06 2.520314e+06 2.538842e+07 9.619936e+07 1.236997e+06 NaN 3.987167e+07 East Asia and Oceania
27 Cameroon NaN 2.807968e+05 3.029649e+05 3.676692e+06 2.027834e+06 5.544217e+07 3.013918e+07 NaN 2.586790e+06 Sub-Saharan Africa
28 Canada NaN NaN 3.944426e+03 NaN NaN NaN NaN NaN 4.740047e+05 Western Hemisphere
29 Caribbean Region NaN 1.232950e+06 6.334080e+06 4.171755e+06 8.930512e+06 3.386467e+07 3.259057e+06 NaN 1.487713e+07 Western Hemisphere
... ... ... ... ... ... ... ... ... ... ... ...
153 Sudan NaN 1.623290e+06 NaN NaN 1.320390e+07 1.728187e+06 3.781959e+08 NaN 1.068070e+07 Sub-Saharan Africa
154 Sudan (former) 9.139544e+05 3.172975e+04 1.996454e+06 2.544591e+05 3.781330e+06 8.763822e+06 2.407008e+08 3.791914e+06 1.572475e+07 Sub-Saharan Africa
155 Suriname NaN NaN NaN NaN 3.157338e+02 NaN NaN NaN 4.232622e+03 Western Hemisphere
156 Swaziland NaN NaN NaN 2.292054e+06 1.441139e+05 8.276367e+07 3.019916e+06 NaN 1.206727e+06 Sub-Saharan Africa
157 Switzerland NaN NaN NaN NaN NaN 1.416848e+06 NaN NaN 3.941007e+05 Europe and Eurasia
158 Syria NaN 2.382808e+05 NaN NaN 9.284948e+07 NaN 1.458470e+09 NaN 6.794437e+06 Middle East and North Africa
159 Tajikistan 1.871526e+07 NaN 3.248985e+06 5.882361e+06 2.449764e+07 1.736964e+07 9.852518e+04 1.235627e+06 4.290652e+06 South and Central Asia
160 Tanzania 8.379234e+07 1.435167e+07 4.715899e+07 3.273734e+07 1.325743e+07 8.039658e+08 9.781955e+06 9.545648e+07 6.547476e+07 Sub-Saharan Africa
161 Thailand NaN NaN NaN 5.734602e+06 1.810380e+07 1.988337e+07 6.296907e+07 6.601187e+03 1.417195e+06 East Asia and Oceania
162 Timor-Leste 3.667280e+06 2.375746e+07 2.184950e+06 2.981390e+06 1.545731e+07 6.600830e+06 3.507495e+06 NaN 1.221403e+07 East Asia and Oceania
163 Togo NaN NaN NaN 1.620912e+06 3.034772e+05 2.591279e+06 NaN NaN 1.870264e+06 Sub-Saharan Africa
164 Tonga NaN NaN NaN 1.731654e+06 NaN NaN 9.297743e+05 NaN NaN East Asia and Oceania
165 Trinidad and Tobago NaN NaN NaN NaN NaN NaN NaN NaN 4.926259e+04 Western Hemisphere
166 Tunisia NaN NaN 6.261098e+06 NaN 3.562968e+07 5.367121e+04 NaN NaN 2.004930e+06 Middle East and North Africa
167 Turkey NaN 1.722756e+05 6.653816e+04 2.475169e+05 4.510667e+05 3.479333e+05 1.603414e+08 2.399003e+06 7.464268e+05 Europe and Eurasia
168 Turkmenistan 5.933679e+02 NaN 4.019272e+06 2.230945e+05 3.642282e+06 1.526698e+06 NaN 9.605331e+05 2.004305e+06 South and Central Asia
169 Uganda 7.860588e+07 1.700393e+07 6.610287e+06 3.485640e+07 1.866597e+07 6.723724e+08 9.516663e+07 1.132422e+07 5.594607e+07 Sub-Saharan Africa
170 Ukraine 3.431983e+06 NaN 9.266748e+06 5.071819e+06 1.094211e+08 4.697222e+07 1.275243e+07 4.642676e+06 5.030748e+07 Europe and Eurasia
171 Uruguay NaN NaN 1.880464e+05 4.700834e+04 1.976440e+04 NaN NaN NaN 8.026157e+04 Western Hemisphere
172 Uzbekistan 6.439760e+05 3.670063e+05 6.139935e+06 1.093630e+05 5.762711e+06 7.145383e+06 NaN NaN 2.474078e+06 South and Central Asia
173 Vanuatu NaN NaN NaN 2.681191e+06 6.253620e+03 1.757286e+06 1.143126e+06 NaN 1.846901e+05 East Asia and Oceania
174 Venezuela NaN NaN NaN NaN 5.583195e+06 NaN 2.098542e+06 NaN 1.561172e+06 Western Hemisphere
175 Vietnam NaN NaN 9.169212e+06 3.312014e+06 1.329477e+07 8.353624e+07 3.029380e+06 3.773216e+06 6.874704e+07 East Asia and Oceania
176 West Africa Region 3.680867e+07 1.990721e+06 1.287467e+07 1.081367e+06 5.067408e+07 4.694186e+07 2.466948e+08 3.323716e+06 3.651115e+07 Sub-Saharan Africa
177 West Bank/Gaza 5.489504e+06 NaN 2.206180e+07 2.363023e+07 2.421555e+08 5.887739e+07 3.559090e+08 4.807701e+07 5.738705e+07 Middle East and North Africa
178 Western Hemisphere Region 3.334605e+05 NaN 1.283435e+06 NaN NaN NaN NaN NaN 1.042143e+06 Western Hemisphere
179 World 5.366879e+08 1.018187e+08 1.519282e+08 2.780233e+08 9.175380e+08 7.944769e+09 1.054667e+09 2.033002e+07 6.962158e+09 World
180 Yemen 1.627746e+07 5.062204e+05 3.035532e+06 2.346827e+07 6.556413e+07 1.523047e+07 2.250901e+08 1.436602e+05 1.796912e+07 Middle East and North Africa
181 Zambia 2.889999e+07 NaN 3.574699e+06 2.694545e+07 7.785643e+06 8.484630e+08 4.762765e+06 3.694694e+04 7.803364e+07 Sub-Saharan Africa
182 Zimbabwe 8.189685e+06 3.330206e+07 8.155591e+06 9.609358e+04 2.873430e+07 1.976670e+08 4.380501e+07 NaN 2.654714e+07 Sub-Saharan Africa

183 rows × 11 columns


In [53]:
span_country_costs = {}
for category in categories:
    span_country_costs[category] = costs_of(long_aid, category, 'country_name', 'constant_amount')
    
span_country_costs_df = pd.DataFrame.from_dict(span_country_costs)
span_country_costs_df.index.name = 'Country'
span_country_costs_df = span_country_costs_df.reset_index(level=0)

span_regions = {}
for index,row in long_aid.iterrows():
    span_region = row['region_name']
    span_country = row['country_name']
    span_regions[span_country] = span_region

span_country_costs_df['Region'] = span_country_costs_df['Country']
span_country_costs_df['Region'] = span_country_costs_df['Region'].replace(regions)

In [55]:
def to_int(x):
    if np.isnan(x):
        return np.nan
    else:
        return int(x)
span_country_costs_df[categories] = span_country_costs_df[categories].applymap(to_int)
span_country_costs_df


Out[55]:
Country Agriculture Commodity Assistance Economic Growth Education Governance Health and Population Humanitarian Infrastructure Other Region
0 Afghanistan 3944025380 684805776 4790284957 2516267153 1.137418e+11 2686194196 4238524384 11287213390 3569695808 South and Central Asia
1 Albania 21044888 44250961 219618434 7340413 5.499558e+08 99872995 30664544 38112952 396187924 Europe and Eurasia
2 Algeria 3259 9489125 6484213 NaN 5.615806e+07 1191609 137822424 6421871 1182031 Middle East and North Africa
3 Angola 19446599 190518493 21393108 8830197 4.262185e+08 828073680 1037871070 4988815 136669730 Sub-Saharan Africa
4 Anguilla 40000 NaN NaN NaN 1.620000e+02 NaN NaN NaN NaN Anguilla
5 Antigua and Barbuda NaN NaN 13892 NaN 2.912464e+07 NaN 453243 NaN 454709 Antigua and Barbuda
6 Argentina 3874494 NaN 11254183 763689 1.095558e+08 1358484 558812 21354318 9396659 Western Hemisphere
7 Armenia 396583787 64888900 138665195 28563704 1.145387e+09 192567391 41556110 225157088 762001777 Europe and Eurasia
8 Aruba NaN NaN 50863 NaN 2.971420e+05 NaN NaN NaN NaN Aruba
9 Asia Region 11448491 1941021 73668330 47184284 1.383104e+08 383026941 85824881 17250086 4336707061 World
10 Asia, Middle East and North Africa Region 17976148 5149749 12921876 66015345 8.805262e+07 347872527 372157738 8561160 369594162 World
11 Australia 130413 NaN NaN NaN 1.268621e+06 8857 21937 2258070 1762828 Australia
12 Austria 10700 NaN NaN NaN 1.327998e+07 NaN NaN 9026951 28422 Austria
13 Azerbaijan 11806644 60091192 288456878 21042776 7.990247e+08 82570929 54843448 70601559 291197509 Europe and Eurasia
14 Bahamas NaN NaN NaN NaN 2.282461e+08 506106 2922825 16761639 1002601 Bahamas
15 Bahrain NaN NaN 213321 NaN 7.413821e+08 NaN 30649 NaN NaN Middle East and North Africa
16 Bangladesh 357353390 1075943441 49732429 116387947 5.120111e+08 1155971905 301055769 91063022 490309978 South and Central Asia
17 Barbados 514 NaN 598172 NaN 2.901145e+07 1411942 4679630 53733 917690 Western Hemisphere
18 Belarus 614467 2808112 12507331 9745710 2.759099e+08 3193600 267397 107702867 89927094 Europe and Eurasia
19 Belgium NaN NaN 253 NaN 4.126436e+07 NaN 544770 151260693 NaN Belgium
20 Belize 933210 NaN 308875 9674406 9.713586e+07 23270368 7545552 NaN 36604112 Western Hemisphere
21 Benin 93547456 64613389 106158893 184567550 8.151615e+07 415393947 14620731 442009309 332725901 Sub-Saharan Africa
22 Bermuda 58158 NaN NaN NaN NaN NaN NaN 66295 27792 Bermuda
23 Bhutan 52800 1057858 NaN NaN 1.501544e+06 2066385 3483108 426545 535488 South and Central Asia
24 Bolivia 1053140440 402690118 46110859 2264936 1.920795e+09 451886121 4799694 3542317 447245924 Western Hemisphere
25 Bosnia and Herzegovina 28658111 116481363 297071522 1694020 1.170526e+09 437744 102760869 10587526 350792133 Europe and Eurasia
26 Botswana 4791180 NaN 26734784 16090 4.968913e+07 1850055083 535467 2993305 29853667 Sub-Saharan Africa
27 Brazil 51342283 NaN 43387741 4672912 3.186422e+08 202420824 4581156 60778148 307985040 Western Hemisphere
28 British Indian Ocean Territory NaN NaN NaN NaN NaN NaN NaN NaN 22530 British Indian Ocean Territory
29 British Virgin Islands NaN NaN NaN NaN 4.104400e+04 NaN NaN NaN NaN British Virgin Islands
... ... ... ... ... ... ... ... ... ... ... ...
212 Tajikistan 102084662 207514894 106850951 36484498 5.917612e+08 176060711 174702875 17328451 201578261 South and Central Asia
213 Tanzania 216716202 137967038 211766317 163861925 3.127683e+08 5434420756 517538135 1370163471 670047831 Sub-Saharan Africa
214 Thailand 2089641 31815 9595302 19831384 6.051362e+08 270280905 549390826 41455772 70979024 East Asia and Oceania
215 Timor-Leste 45835623 78866475 70480487 6954413 3.467976e+08 103275153 82867369 61469 204893147 East Asia and Oceania
216 Togo 63260 10953759 NaN 8795957 3.815815e+07 17909470 3113179 906714 58011502 Sub-Saharan Africa
217 Tonga NaN NaN NaN 8876049 1.245465e+07 105185 1752362 NaN 23263013 East Asia and Oceania
218 Trinidad and Tobago 255494 NaN 2706421 NaN 2.721990e+07 2214599 1875211 1384416 723262 Western Hemisphere
219 Tunisia 1562907 NaN 67747876 NaN 7.308423e+08 561542 9857812 2748344 8918132 Middle East and North Africa
220 Turkey 1245300 172275 25865893 1506871 6.763750e+08 7153122 353368011 45338938 502436601 Europe and Eurasia
221 Turkmenistan 5597118 4041226 37663415 15065742 1.708628e+08 47078099 289128 5180380 122380500 South and Central Asia
222 Turks and Caicos Islands NaN NaN NaN NaN 2.200940e+05 NaN NaN NaN NaN Turks and Caicos Islands
223 Tuvalu NaN NaN NaN NaN 1.652230e+05 NaN 101509 21244 NaN Tuvalu
224 Uganda 498121656 410096586 96729637 395352761 4.276770e+08 5442034797 1250015592 72990848 522479720 Sub-Saharan Africa
225 Ukraine 28222474 4402641 665367217 60215452 2.224055e+09 435216655 23710884 863159826 1475316844 Europe and Eurasia
226 United Arab Emirates NaN NaN 4696571 NaN 1.503076e+07 74287 63488 15089151 111424 United Arab Emirates
227 United Kingdom 303637 NaN 363502 NaN 1.888560e+07 15646 NaN 48741346 1111625 United Kingdom
228 Uruguay 1462617 NaN 1682647 157834 2.509331e+07 NaN 1563806 6454610 1421847 Western Hemisphere
229 Uzbekistan 19215697 159374022 101980358 9151704 5.088903e+08 187987262 9972314 212667273 458332264 South and Central Asia
230 Vanuatu 37221 NaN NaN 11207777 4.588281e+06 7258733 3397411 137346611 85740426 East Asia and Oceania
231 Venezuela 2407350 NaN 3301459 NaN 3.066297e+08 4514666 3325761 4033599 13668389 Western Hemisphere
232 Vietnam 5815855 102597211 136643547 11497934 3.410246e+08 1086353707 44869462 118022889 232748467 East Asia and Oceania
233 West Africa Region 286900030 121380109 102421205 50178664 3.394714e+08 490454130 318720441 32212827 216025750 Sub-Saharan Africa
234 West Bank/Gaza 58722781 11132381 539887556 326745810 5.570996e+09 1676061798 2866778398 582255101 838085212 Middle East and North Africa
235 Western (Spanish) Sahara NaN NaN NaN NaN NaN NaN 2229102 NaN NaN Western (Spanish) Sahara
236 Western Europe Region NaN NaN NaN NaN 1.555400e+04 NaN 2476739 729541 NaN Western Europe Region
237 Western Hemisphere Region 610357 NaN 4946883 NaN 5.800695e+06 NaN 2745359 NaN 17622446 Western Hemisphere
238 World 2917788917 1899042297 1795873770 1537330367 2.235223e+10 71218419061 13790023373 577687783 78565505203 World
239 Yemen 46953136 328359865 33974370 168564506 7.238324e+08 178498294 922947120 8418742 79217661 Middle East and North Africa
240 Zambia 224038432 110742831 98465688 257003446 2.122428e+08 4511826113 101198254 3400020 402730098 Sub-Saharan Africa
241 Zimbabwe 111332380 64493117 39438658 2568939 5.421860e+08 1043810160 1436088719 NaN 194407526 Sub-Saharan Africa

242 rows × 11 columns


In [72]:
cats = ['Agriculture',
 'Commodity Assistance',
 'Economic Growth',
 'Education',
 'Governance',
 'Health and Population',
 'Humanitarian',
 'Infrastructure',
 'Other']
def totals_for_cats(df):
    spending = {}
    for index, row in df.iterrows():
        cat = row['dac_category_name']
        code = row['country_code']
        if cat in cats and code != 'WLD' and code != '':
            name = row['country_name']
            amount = row['constant_amount']
            if amount >= 0:
                if code in spending:
                    spending[code]['2014 ' + cat] += amount
                else:
                    spending[code] = {'name': name}
                    for c in cats:
                        spending[code]['2014 ' + c] = 0
                    spending[code]['2014 ' + cat] = amount
    return spending

In [73]:
aid = aid.dropna(axis=0, subset=['country_code'])
totalsEachCat = totals_for_cats(aid)

In [75]:
totalsdf = pd.DataFrame.from_dict(totalsEachCat, orient='index')
totalsdf.to_csv('data/aid_FY2014_condensed.csv', encoding='utf-8')

In [47]:
span_country_costs_df.columns = ['2000-2014 ' + col for col in span_country_costs_df.columns]

In [48]:
span_country_costs_df = span_country_costs_df.drop('2000-2014 Region', 1)
span_country_costs_df = span_country_costs_df.rename( columns={'2000-2014 Country': 'Country'})

In [49]:
span_country_costs_df


Out[49]:
Country 2000-2014 Agriculture 2000-2014 Commodity Assistance 2000-2014 Economic Growth 2000-2014 Education 2000-2014 Governance 2000-2014 Health and Population 2000-2014 Humanitarian 2000-2014 Infrastructure 2000-2014 Other
0 Afghanistan 3944025380 684805776 4790284957 2516267153 1.137418e+11 2686194196 4238524384 11287213390 3569695808
1 Albania 21044888 44250961 219618434 7340413 5.499558e+08 99872995 30664544 38112952 396187924
2 Algeria 3259 9489125 6484213 NaN 5.615806e+07 1191609 137822424 6421871 1182031
3 Angola 19446599 190518493 21393108 8830197 4.262185e+08 828073680 1037871070 4988815 136669730
4 Anguilla 40000 NaN NaN NaN 1.620000e+02 NaN NaN NaN NaN
5 Antigua and Barbuda NaN NaN 13892 NaN 2.912464e+07 NaN 453243 NaN 454709
6 Argentina 3874494 NaN 11254183 763689 1.095558e+08 1358484 558812 21354318 9396659
7 Armenia 396583787 64888900 138665195 28563704 1.145387e+09 192567391 41556110 225157088 762001777
8 Aruba NaN NaN 50863 NaN 2.971420e+05 NaN NaN NaN NaN
9 Asia Region 11448491 1941021 73668330 47184284 1.383104e+08 383026941 85824881 17250086 4336707061
10 Asia, Middle East and North Africa Region 17976148 5149749 12921876 66015345 8.805262e+07 347872527 372157738 8561160 369594162
11 Australia 130413 NaN NaN NaN 1.268621e+06 8857 21937 2258070 1762828
12 Austria 10700 NaN NaN NaN 1.327998e+07 NaN NaN 9026951 28422
13 Azerbaijan 11806644 60091192 288456878 21042776 7.990247e+08 82570929 54843448 70601559 291197509
14 Bahamas NaN NaN NaN NaN 2.282461e+08 506106 2922825 16761639 1002601
15 Bahrain NaN NaN 213321 NaN 7.413821e+08 NaN 30649 NaN NaN
16 Bangladesh 357353390 1075943441 49732429 116387947 5.120111e+08 1155971905 301055769 91063022 490309978
17 Barbados 514 NaN 598172 NaN 2.901145e+07 1411942 4679630 53733 917690
18 Belarus 614467 2808112 12507331 9745710 2.759099e+08 3193600 267397 107702867 89927094
19 Belgium NaN NaN 253 NaN 4.126436e+07 NaN 544770 151260693 NaN
20 Belize 933210 NaN 308875 9674406 9.713586e+07 23270368 7545552 NaN 36604112
21 Benin 93547456 64613389 106158893 184567550 8.151615e+07 415393947 14620731 442009309 332725901
22 Bermuda 58158 NaN NaN NaN NaN NaN NaN 66295 27792
23 Bhutan 52800 1057858 NaN NaN 1.501544e+06 2066385 3483108 426545 535488
24 Bolivia 1053140440 402690118 46110859 2264936 1.920795e+09 451886121 4799694 3542317 447245924
25 Bosnia and Herzegovina 28658111 116481363 297071522 1694020 1.170526e+09 437744 102760869 10587526 350792133
26 Botswana 4791180 NaN 26734784 16090 4.968913e+07 1850055083 535467 2993305 29853667
27 Brazil 51342283 NaN 43387741 4672912 3.186422e+08 202420824 4581156 60778148 307985040
28 British Indian Ocean Territory NaN NaN NaN NaN NaN NaN NaN NaN 22530
29 British Virgin Islands NaN NaN NaN NaN 4.104400e+04 NaN NaN NaN NaN
... ... ... ... ... ... ... ... ... ... ...
212 Tajikistan 102084662 207514894 106850951 36484498 5.917612e+08 176060711 174702875 17328451 201578261
213 Tanzania 216716202 137967038 211766317 163861925 3.127683e+08 5434420756 517538135 1370163471 670047831
214 Thailand 2089641 31815 9595302 19831384 6.051362e+08 270280905 549390826 41455772 70979024
215 Timor-Leste 45835623 78866475 70480487 6954413 3.467976e+08 103275153 82867369 61469 204893147
216 Togo 63260 10953759 NaN 8795957 3.815815e+07 17909470 3113179 906714 58011502
217 Tonga NaN NaN NaN 8876049 1.245465e+07 105185 1752362 NaN 23263013
218 Trinidad and Tobago 255494 NaN 2706421 NaN 2.721990e+07 2214599 1875211 1384416 723262
219 Tunisia 1562907 NaN 67747876 NaN 7.308423e+08 561542 9857812 2748344 8918132
220 Turkey 1245300 172275 25865893 1506871 6.763750e+08 7153122 353368011 45338938 502436601
221 Turkmenistan 5597118 4041226 37663415 15065742 1.708628e+08 47078099 289128 5180380 122380500
222 Turks and Caicos Islands NaN NaN NaN NaN 2.200940e+05 NaN NaN NaN NaN
223 Tuvalu NaN NaN NaN NaN 1.652230e+05 NaN 101509 21244 NaN
224 Uganda 498121656 410096586 96729637 395352761 4.276770e+08 5442034797 1250015592 72990848 522479720
225 Ukraine 28222474 4402641 665367217 60215452 2.224055e+09 435216655 23710884 863159826 1475316844
226 United Arab Emirates NaN NaN 4696571 NaN 1.503076e+07 74287 63488 15089151 111424
227 United Kingdom 303637 NaN 363502 NaN 1.888560e+07 15646 NaN 48741346 1111625
228 Uruguay 1462617 NaN 1682647 157834 2.509331e+07 NaN 1563806 6454610 1421847
229 Uzbekistan 19215697 159374022 101980358 9151704 5.088903e+08 187987262 9972314 212667273 458332264
230 Vanuatu 37221 NaN NaN 11207777 4.588281e+06 7258733 3397411 137346611 85740426
231 Venezuela 2407350 NaN 3301459 NaN 3.066297e+08 4514666 3325761 4033599 13668389
232 Vietnam 5815855 102597211 136643547 11497934 3.410246e+08 1086353707 44869462 118022889 232748467
233 West Africa Region 286900030 121380109 102421205 50178664 3.394714e+08 490454130 318720441 32212827 216025750
234 West Bank/Gaza 58722781 11132381 539887556 326745810 5.570996e+09 1676061798 2866778398 582255101 838085212
235 Western (Spanish) Sahara NaN NaN NaN NaN NaN NaN 2229102 NaN NaN
236 Western Europe Region NaN NaN NaN NaN 1.555400e+04 NaN 2476739 729541 NaN
237 Western Hemisphere Region 610357 NaN 4946883 NaN 5.800695e+06 NaN 2745359 NaN 17622446
238 World 2917788917 1899042297 1795873770 1537330367 2.235223e+10 71218419061 13790023373 577687783 78565505203
239 Yemen 46953136 328359865 33974370 168564506 7.238324e+08 178498294 922947120 8418742 79217661
240 Zambia 224038432 110742831 98465688 257003446 2.122428e+08 4511826113 101198254 3400020 402730098
241 Zimbabwe 111332380 64493117 39438658 2568939 5.421860e+08 1043810160 1436088719 NaN 194407526

242 rows × 10 columns


In [24]:
def plot_costs(costs, x, y, xlabel, ylabel):
    plt.figure(figsize=(12, 26))
    cost_plot = sns.barplot(data=costs.sort(x, ascending=False), x=x, y=y)
    cost_plot.set(xlabel=xlabel, ylabel=ylabel)
    sns.despine(left=True, bottom=True)


plot_costs(totals_df, 'Cost', 'Category', 'Total Money Spent in 2014', 'US Foreign Expenditure Category')


C:\Anaconda\lib\site-packages\ipykernel\__main__.py:3: FutureWarning: sort(columns=....) is deprecated, use sort_values(by=.....)
  app.launch_new_instance()
C:\Anaconda\lib\site-packages\matplotlib\__init__.py:892: UserWarning: axes.color_cycle is deprecated and replaced with axes.prop_cycle; please use the latter.
  warnings.warn(self.msg_depr % (key, alt_key))

In [25]:
hdr = pd.read_excel('data/HDR.xlsx')
hdr


Out[25]:
Country 2000 Life Expectancy 2013 Life Expectancy 2000-2013 Life Expectancy Increase 2000 Education Index 2013 Education Index 2000-2013 Education Index Increase 2000 Mean Years of Schooling 2013 Mean Years of Schooling 2000-2013 Mean Years of Schooling Increase 2000 Income Index 2013 Income Index 2000-2013 Income Index Increase 2000 GNI per Capita 2013 GNI per Capita 2000-2013 GNI per Capita Increase 2000 HDI 2013 HDI 2000-2013 HDI Increase
0 Afghanistan 54.9 60.9 6.0 0.225522 0.365333 0.139811 2.1 3.2 1.1 0.328 0.445 0.117 877 1904 1027 0.341 0.468 0.127
1 Albania 74.2 77.4 3.2 0.565465 0.608519 0.043054 8.5 9.3 0.8 0.596 0.683 0.087 5185 9225 4040 0.655 0.716 0.061
2 Algeria 69.0 71.0 2.0 0.493489 0.642589 0.149100 5.9 7.6 1.7 0.686 0.730 0.044 9378 12555 3177 0.634 0.717 0.083
3 Andorra 78.9 81.2 2.3 NaN 0.670287 NaN NaN 10.4 NaN 0.867 0.907 0.040 31171 40597 9426 NaN 0.830 NaN
4 Angola 45.2 51.9 6.7 0.298712 0.474212 0.175500 4.4 4.7 0.3 0.461 0.626 0.165 2117 6323 4206 0.377 0.526 0.149
5 Antigua and Barbuda 73.5 76.0 2.5 NaN 0.681141 NaN NaN 8.9 NaN 0.750 0.791 0.041 14289 18800 4511 NaN 0.774 NaN
6 Argentina 73.8 76.3 2.5 0.710665 0.783356 0.072690 9.1 9.8 0.7 0.724 0.778 0.054 12088 17297 5209 0.753 0.808 0.055
7 Armenia 71.5 74.6 3.1 0.668267 0.701367 0.033100 10.8 10.8 0.0 0.514 0.661 0.147 3004 7952 4948 0.648 0.730 0.082
8 Australia 79.6 82.5 2.9 0.895400 0.926536 0.031136 11.9 12.8 0.9 0.881 0.911 0.030 34148 41524 7376 0.898 0.933 0.035
9 Austria 78.1 81.1 3.0 0.729521 0.794481 0.064960 9.1 10.8 1.7 0.894 0.916 0.022 37079 42930 5851 0.835 0.881 0.046
10 Azerbaijan 66.9 70.8 3.9 0.637533 0.699778 0.062245 10.6 11.2 0.6 0.569 0.764 0.195 4320 15725 11405 0.639 0.747 0.108
11 Bahamas 72.4 75.2 2.8 0.668041 0.713955 0.045914 10.9 10.9 0.0 0.835 0.811 -0.024 25172 21414 -3758 0.766 0.789 0.023
12 Bahrain 74.5 76.6 2.1 0.656139 0.713967 0.057828 8.3 9.4 1.1 0.875 0.872 -0.003 32809 32072 -737 0.784 0.815 0.031
13 Bangladesh 65.3 70.7 5.4 0.330374 0.446880 0.116506 3.7 5.1 1.4 0.403 0.499 0.096 1444 2713 1269 0.453 0.558 0.105
14 Barbados 73.1 75.4 2.3 0.683811 0.739778 0.055967 8.7 9.4 0.7 0.741 0.742 0.001 13485 13604 119 0.745 0.776 0.031
15 Belarus 67.4 69.9 2.5 NaN 0.819984 NaN NaN 11.5 NaN 0.652 0.770 0.118 7468 16403 8935 NaN 0.786 NaN
16 Belgium 77.8 80.5 2.7 0.834773 0.812292 -0.022482 10.0 10.9 0.9 0.896 0.903 0.007 37590 39471 1881 0.873 0.881 0.008
17 Belize 70.4 73.9 3.5 0.603430 0.688889 0.085459 8.8 9.3 0.5 0.656 0.686 0.030 7676 9364 1688 0.675 0.732 0.057
18 Benin 55.3 59.3 4.0 0.268700 0.413556 0.144856 2.6 3.2 0.6 0.409 0.430 0.021 1503 1726 223 0.391 0.476 0.085
19 Bhutan 60.3 68.3 8.0 NaN 0.421244 NaN NaN 2.3 NaN 0.541 0.637 0.096 3597 6775 3178 NaN 0.584 NaN
20 Bolivia (Plurinational State of) 63.0 67.3 4.3 0.620389 0.673500 0.053111 7.4 9.2 1.8 0.566 0.607 0.041 4238 5552 1314 0.615 0.667 0.052
21 Bosnia and Herzegovina 74.9 76.4 1.5 NaN 0.655318 NaN NaN 8.3 NaN 0.635 0.687 0.052 6707 9431 2724 NaN 0.731 NaN
22 Botswana 48.7 64.4 15.7 0.576100 0.619000 0.042900 7.5 8.8 1.3 0.690 0.755 0.065 9611 14792 5181 0.560 0.683 0.123
23 Brazil 70.3 73.9 3.6 0.581496 0.661306 0.079810 5.6 7.2 1.6 0.706 0.749 0.043 10722 14275 3553 0.682 0.744 0.062
24 Brunei Darussalam 75.9 78.5 2.6 0.646456 0.692111 0.045656 8.2 8.7 0.5 1.000 0.991 -0.009 75189 70883 -4306 0.822 0.852 0.030
25 Bulgaria 71.4 73.5 2.1 0.676223 0.749407 0.073184 9.5 10.6 1.1 0.680 0.761 0.081 8994 15402 6408 0.714 0.777 0.063
26 Burkina Faso 50.6 56.3 5.7 NaN 0.250033 NaN NaN 1.3 NaN 0.357 0.419 0.062 1062 1602 540 NaN 0.388 NaN
27 Burundi 48.3 54.1 5.8 0.186628 0.370089 0.183461 1.8 2.7 0.9 0.301 0.304 0.003 735 749 14 0.290 0.389 0.099
28 Cambodia 62.0 71.9 9.9 0.400311 0.495178 0.094867 5.7 5.8 0.1 0.391 0.504 0.113 1328 2805 1477 0.466 0.584 0.118
29 Cameroon 52.0 55.1 3.1 0.356522 0.485789 0.129267 4.8 5.9 1.1 0.464 0.490 0.026 2151 2557 406 0.433 0.504 0.071
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
157 Sudan 58.0 62.1 4.1 0.206000 0.305956 0.099956 2.4 3.1 0.7 0.475 0.534 0.059 2327 3428 1101 0.385 0.473 0.088
158 Suriname 67.9 71.0 3.1 NaN 0.588380 NaN NaN 7.7 NaN 0.658 0.758 0.100 7788 15113 7325 NaN 0.705 NaN
159 Swaziland 48.7 49.0 0.3 0.459033 0.551289 0.092256 6.0 7.1 1.1 0.610 0.606 -0.004 5668 5536 -132 0.498 0.530 0.032
160 Sweden 79.7 81.8 2.1 0.866283 0.830134 -0.036149 11.0 11.7 0.7 0.883 0.917 0.034 34614 43201 8587 0.889 0.898 0.009
161 Switzerland 79.9 82.6 2.7 0.803632 0.844089 0.040457 11.4 12.2 0.8 0.939 0.950 0.011 50146 53762 3616 0.886 0.917 0.031
162 Syrian Arab Republic 73.3 74.6 1.3 0.450793 0.553192 0.102398 5.1 6.6 1.5 0.598 0.613 0.015 5231 5771 540 0.605 0.658 0.053
163 Tajikistan 63.6 67.2 3.6 0.600644 0.639444 0.038800 9.9 9.9 0.0 0.367 0.482 0.115 1134 2424 1290 0.529 0.607 0.078
164 Tanzania (United Republic of) 50.0 61.5 11.5 0.324919 0.425889 0.100970 4.6 5.1 0.5 0.353 0.428 0.075 1035 1702 667 0.376 0.488 0.112
165 Thailand 70.9 74.4 3.5 0.515787 0.607965 0.092178 6.1 7.3 1.2 0.676 0.739 0.063 8771 13364 4593 0.649 0.722 0.073
166 The former Yugoslav Republic of Macedonia 73.2 75.2 2.0 NaN 0.641898 NaN NaN 8.2 NaN 0.678 0.720 0.042 8906 11745 2839 NaN 0.732 NaN
167 Timor-Leste 59.5 67.5 8.0 0.361503 0.472407 0.110904 2.8 4.4 1.6 0.457 0.691 0.234 2056 9674 7618 0.465 0.620 0.155
168 Togo 53.6 56.5 2.9 0.406911 0.514422 0.107511 4.4 5.3 0.9 0.378 0.366 -0.012 1222 1129 -93 0.430 0.473 0.043
169 Tonga 70.8 72.7 1.9 0.663667 0.720333 0.056667 8.9 9.4 0.5 0.585 0.600 0.015 4796 5316 520 0.672 0.705 0.033
170 Trinidad and Tobago 68.5 69.9 1.4 0.606569 0.700321 0.093752 8.3 10.8 2.5 0.747 0.836 0.089 14070 25325 11255 0.697 0.766 0.069
171 Tunisia 73.2 75.9 2.7 0.525222 0.621456 0.096233 4.8 6.5 1.7 0.647 0.702 0.055 7250 10440 3190 0.653 0.721 0.068
172 Turkey 70.0 75.3 5.3 0.493167 0.652024 0.158857 5.5 7.6 2.1 0.734 0.788 0.054 12890 18391 5501 0.653 0.759 0.106
173 Turkmenistan 63.9 65.5 1.6 NaN 0.678713 NaN 9.9 9.9 0.0 0.608 0.717 0.109 5615 11533 5918 NaN 0.698 NaN
174 Uganda 48.1 59.2 11.1 0.425922 0.478644 0.052721 3.9 5.4 1.5 0.326 0.391 0.065 867 1335 468 0.392 0.484 0.092
175 Ukraine 67.3 68.5 1.2 0.705267 0.795511 0.090244 10.7 11.3 0.6 0.581 0.666 0.085 4672 8215 3543 0.668 0.734 0.066
176 United Arab Emirates 74.2 76.8 2.6 0.604148 0.673215 0.069067 8.3 9.1 0.8 1.000 0.961 -0.039 103798 58068 -45730 0.797 0.827 0.030
177 United Kingdom 77.7 80.5 2.8 0.835551 0.860260 0.024709 11.6 12.3 0.7 0.868 0.885 0.017 31296 35002 3706 0.863 0.892 0.029
178 United States 76.8 78.9 2.1 0.849882 0.889759 0.039877 12.7 12.9 0.2 0.928 0.946 0.018 46551 52308 5757 0.883 0.914 0.031
179 Uruguay 74.8 77.2 2.4 0.660484 0.712262 0.051778 8.0 8.5 0.5 0.728 0.785 0.057 12400 18108 5708 0.740 0.790 0.050
180 Uzbekistan 67.0 68.2 1.2 NaN 0.651111 NaN NaN 10.0 NaN 0.483 0.598 0.115 2442 5227 2785 NaN 0.661 NaN
181 Vanuatu 67.4 71.6 4.2 NaN 0.595511 NaN NaN 9.0 NaN 0.488 0.495 0.007 2535 2652 117 NaN 0.616 NaN
182 Venezuela (Bolivarian Republic of) 72.4 74.6 2.2 0.513255 0.682483 0.169228 6.6 8.6 2.0 0.751 0.776 0.025 14417 17067 2650 0.677 0.764 0.087
183 Viet Nam 73.8 75.9 2.1 0.434456 0.513489 0.079033 4.5 5.5 1.0 0.493 0.588 0.095 2615 4892 2277 0.563 0.638 0.075
184 Yemen 60.4 63.1 2.7 0.257439 0.339222 0.081783 1.1 2.5 1.4 0.487 0.555 0.068 2507 3945 1438 0.427 0.500 0.073
185 Zambia 41.8 58.1 16.3 0.487518 0.591333 0.103815 5.9 6.5 0.6 0.463 0.509 0.046 2138 2898 760 0.423 0.561 0.138
186 Zimbabwe 44.0 59.9 15.9 0.471333 0.499933 0.028600 5.9 7.2 1.3 0.451 0.388 -0.063 1984 1307 -677 0.428 0.492 0.064

187 rows × 19 columns


In [26]:
populations = pd.read_csv('data/2014_populations.csv')
foreign_aid = pd.merge(hdr,country_costs_df, on='Country', how='outer')
foreign_aid = pd.merge(foreign_aid,span_country_costs_df, on='Country', how='outer')
foreign_aid = foreign_aid.rename(columns={'Region_y': 'Region'})

for category in ['2014 ' + cat for cat in categories]:
    foreign_aid[category] = foreign_aid[category].fillna(0)
    
for category in ['2000-2014 ' + cat for cat in categories]:
    foreign_aid[category] = foreign_aid[category].fillna(0)


foreign_aid = pd.merge(foreign_aid, populations, on='Country', how='outer')
foreign_aid = foreign_aid.dropna(subset=['2013 Life Expectancy'])


foreign_aid.ix[foreign_aid.Country==u"C\xf4te d'Ivoire", 'Country'] = "Ivory Coast"

In [27]:
europe = 'Europe and Eurasia'
asia = 'East Asia and Oceania'
n_america = 'North America'
s_america = 'South and Central America'
middle_east = 'Middle East and North Africa'
s_africa = 'Sub-Saharan Africa'
regions = dict(zip(foreign_aid.Country, foreign_aid.Region))

In [28]:
for country in regions:
    if regions[country] == 'Europe and Eurasia':
        regions[country] = europe
    if regions[country] == 'East Asia and Oceania':
        regions[country] = asia
    if regions[country] == 'Western Hemisphere':
        regions[country] = s_america

regions['Australia'] = asia
regions['New Zealand'] = asia
regions['Korea'] = asia
regions['China'] = asia
regions['Austria'] = europe
regions['Belgium'] = europe
regions['Bolivia'] = s_america
regions['Burma'] = asia
regions['Ceylon'] = asia
regions['Czechoslovakia'] = europe
regions['Dominican Rep'] = s_america
regions['France'] = europe
regions['Iran'] = middle_east
regions['Italy'] = europe
regions['Laos'] = asia
regions['Malaya'] = asia
regions['Portugal'] = europe
regions['Saudi Arabia'] = middle_east
regions['Spain'] = europe
regions['Syria'] = middle_east
regions['South Africa'] = s_africa
regions['United states'] = n_america
regions['Canada'] = n_america
regions['Venezuala'] = s_america
regions['Germany'] = europe
regions['Yugoslavia'] = europe

regions['Andorra'] = europe
regions['Antigua and Barbuda'] = s_america
regions['Bahamas'] = s_america
regions['Bolivia (Plurinational State of)'] = s_america
regions['Brunei Darussalam'] = asia
regions['Cape Verde'] = s_africa
regions['Congo'] = s_africa
regions['Congo (Democratic Republic of the)'] = s_africa
regions['Ivory Coast'] = s_africa
regions['Denmark'] = europe
regions['Dominica'] = s_america
regions['Equatorial Guinea'] = s_africa
regions['Eritrea'] = middle_east
regions['Finland'] = europe
regions['Grenada'] = s_america
regions['Guinea-Bissau'] = s_africa
regions['Hong Kong, China (SAR)'] = asia
regions['Iceland'] = europe
regions['Iran (Islamic Republic of)'] = middle_east
regions['Kiribati'] = asia
regions['Korea (Republic of)'] = asia
regions['Kuwait'] = middle_east
regions['Lao People\'s Democratic Republic'] = asia
regions['Latvia'] = europe
regions['Liechtenstein'] = europe
regions['Lithuania'] = europe
regions['Luxembourg'] = europe
regions['Micronesia (Federated States of)'] = asia
regions['Moldova (Republic of)'] = europe
regions['Myanmar'] = middle_east
regions['Netherlands'] = europe
regions['Norway'] = europe
regions['Palestine, State of'] = middle_east
regions['Poland'] = europe
regions['Qatar'] = middle_east
regions['Russian Federation'] = europe
regions['Saint Kitts and Nevis'] = s_america
regions['Saint Lucia'] = s_america
regions['Saint Vincent and the Grenadines'] = s_america
regions['Singapore'] = asia
regions['Slovakia'] = europe
regions['Slovenia'] = europe
regions['Suriname'] = s_america
regions['Sweden'] = europe
regions['Syrian Arab Republic'] = middle_east
regions['Tanzania (United Republic of)'] = s_africa
regions['The former Yugoslav Republic of Macedonia'] = europe
regions['United Arab Emirates'] = middle_east
regions['United Kingdom'] = europe
regions['United States'] = n_america
regions['Canada'] = n_america
regions['Venezuela (Bolivarian Republic of)'] = s_america
regions['Viet Nam'] = asia

def map_getter(someMap, column):
    def getter(x):
        key = x[column]

        if key in someMap:
            return (someMap[key])
        else:
            return None
    return getter

foreign_aid = foreign_aid
foreign_aid['Region'] = foreign_aid.apply(map_getter(regions, 0), axis=1)

In [29]:
countries = foreign_aid[pd.isnull(foreign_aid['Region'])]['Country'].unique()
for country in countries:
    print country

In [30]:
foreign_aid['Region'].unique()


Out[30]:
array([u'South and Central Asia', 'Europe and Eurasia',
       u'Middle East and North Africa', u'Sub-Saharan Africa',
       'South and Central America', 'East Asia and Oceania',
       'North America'], dtype=object)

In [31]:
foreign_aid.to_csv('data/merged_aid.csv', index=False)


---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-31-8e12c063756b> in <module>()
      1 
----> 2 foreign_aid.to_csv('data/merged_aid.csv', index=False)

C:\Anaconda\lib\site-packages\pandas\core\frame.pyc in to_csv(self, path_or_buf, sep, na_rep, float_format, columns, header, index, index_label, mode, encoding, compression, quoting, quotechar, line_terminator, chunksize, tupleize_cols, date_format, doublequote, escapechar, decimal, **kwds)
   1330                                      escapechar=escapechar,
   1331                                      decimal=decimal)
-> 1332         formatter.save()
   1333 
   1334         if path_or_buf is None:

C:\Anaconda\lib\site-packages\pandas\core\format.pyc in save(self)
   1479             f = _get_handle(self.path_or_buf, self.mode,
   1480                                 encoding=self.encoding,
-> 1481                                 compression=self.compression)
   1482             close = True
   1483 

C:\Anaconda\lib\site-packages\pandas\io\common.pyc in _get_handle(path, mode, encoding, compression)
    354                 f = open(path, mode, errors='replace')
    355         else:
--> 356             f = open(path, mode)
    357 
    358     return f

IOError: [Errno 13] Permission denied: 'data/merged_aid.csv'

In [122]:
#countries of the world with coordinates data
cowcoords = pd.read_csv('data/cow_cleaned.csv', encoding='utf-8')

In [129]:
def trimStr(x):
    if type(x) is unicode:
        return x.strip()
    else:
        return x
cowcoords = cowcoords.applymap(trimStr)
cowcoords.iloc[0]['Country']


Out[129]:
u'Afghanistan'

In [131]:
newmerged = foreign_aid.merge(cowcoords, how='outer', on='Country')


Out[131]:
Index([                                   u'Country',
                             u'2000 Life Expectancy',
                             u'2013 Life Expectancy',
               u'2000-2013 Life Expectancy Increase',
                             u'2000 Education Index',
                             u'2013 Education Index',
               u'2000-2013 Education Index Increase',
                     u'2000 Mean Years of Schooling',
                     u'2013 Mean Years of Schooling',
       u'2000-2013 Mean Years of Schooling Increase',
       ...
                                      u'Unnamed: 61',
                                      u'Unnamed: 62',
                                      u'Unnamed: 63',
                                      u'Unnamed: 64',
                                      u'Unnamed: 65',
                                      u'Unnamed: 66',
                                      u'Unnamed: 67',
                                      u'Unnamed: 68',
                                      u'Unnamed: 69',
                                      u'Unnamed: 70'],
      dtype='object', length=109)

In [134]:
newmerged.to_csv('data/merged_with_coordinates.csv', encoding='utf-8')

In [114]:
raw_data = {
        'subject_id': ['1', '2', '3', '4', '5'],
        'first_name': ['Alex ', 'Amy ', 'Allen ', 'Alice ', 'Ayoung '],
        'last_name': ['Anderson', 'Ackerman', 'Ali', 'Aoni', 'Atiches']}
df_a = pd.DataFrame(raw_data, columns = ['subject_id', 'first_name', 'last_name'])
df_a


Out[114]:
subject_id first_name last_name
0 1 Alex Anderson
1 2 Amy Ackerman
2 3 Allen Ali
3 4 Alice Aoni
4 5 Ayoung Atiches

In [117]:
def addStar(x):
    if type(x) is str:
        return x.strip()+'*'
    else:
        return x
df_a.applymap(addStar)


Out[117]:
subject_id first_name last_name
0 1* Alex* Anderson*
1 2* Amy* Ackerman*
2 3* Allen* Ali*
3 4* Alice* Aoni*
4 5* Ayoung* Atiches*

In [96]:
raw_data = {
        'subject_id': ['4', '5', '6', '7', '8'],
        'first_name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'],
        'last_name': ['Bonder', 'Black', 'Balwner', 'Brice', 'Btisan']}
df_b = pd.DataFrame(raw_data, columns = ['subject_id', 'first_name', 'last_name'])
df_b


Out[96]:
subject_id first_name last_name
0 4 Billy Bonder
1 5 Brian Black
2 6 Bran Balwner
3 7 Bryce Brice
4 8 Betty Btisan

In [98]:
raw_data = {
        'subject_id': ['1', '2', '3', '4', '5', '7', '8', '9', '10', '11'],
        'test_id': [51, 15, 15, 61, 16, 14, 15, 1, 61, 16]}
df_n = pd.DataFrame(raw_data, columns = ['subject_id','test_id'])
df_new = pd.concat([df_a, df_b])
pd.merge(df_new, df_n, on='subject_id')


Out[98]:
subject_id first_name last_name test_id
0 1 Alex Anderson 51
1 2 Amy Ackerman 15
2 3 Allen Ali 15
3 4 Alice Aoni 61
4 4 Billy Bonder 61
5 5 Ayoung Atiches 16
6 5 Brian Black 16
7 7 Bryce Brice 14
8 8 Betty Btisan 15

In [ ]:
sns.jointplot(kind='reg',x="2014 Economic Growth", y="2013 GNI per Capita", data=foreign_aid)

In [ ]:
sns.jointplot(kind='reg', x="2014 Education", y="2013 Mean Years of Schooling", data=foreign_aid)

In [ ]:
sns.jointplot(kind='reg',x="2014 Health and Population", y="2013 Life Expectancy", data=foreign_aid)