hiv = 'hiv_incidence_rate_1549_years_old_percentage_midpoint'
yearly = pd.DataFrame(undata.groupby(['countryname', 'iso3code', 'year'])[hiv].mean())
hivdf = yearly.unstack()

hivdf['mean'] = hivdf.mean(axis=1, skipna=True)

hivdf = hivdf.sort(columns='mean', ascending=False)
hivdf.head(20)

"""
Explanation: Explore HIV incidence variable and reshape dataframes for possible visualizations
Explore HIV incidence
Group by country/year and unstack to investigate by year
Add a mean HIV incidence column sort by this column to find the 20 countries with the highest average rate over this time period.
End of explanation
"""


hiv_change = hivdf.pct_change(axis=1)

highest = hiv_change.head(20)
highest

"""
Explanation: Investigate percent change from previous year in countries with highest HIV incidence
End of explanation
"""


highest.to_csv('hiv_pctchange.csv')

"""
Explanation: Save to CSV for possible visualization
End of explanation
"""


oda_mill = 'net_oda_mill'
oda_pc = 'net_oda_received_per_capita_current_us'
oda_pct = 'net_oda_received__of_gni'

"""
Explanation: Investigate ODA received by countries with highest HIV incidence rate
ODA = Official Development Assistence
End of explanation
"""


highest_countries = []

for i in range(20):
    highest_countries.append(highest.index[i][0])

def subset_unstack(col, df):
    temp2_df = df
    ## find only the countries with highest HIV incidence
    for country in highest_countries:
        country_df = undata[undata['countryname'] == country]
        temp1_df = country_df[['countryname', 'iso3code', 'year', col]]
        temp2_df = pd.concat([temp2_df, temp1_df])
        
    ## unstack dataframe
    yearly = pd.DataFrame(temp2_df.groupby(['countryname', 'iso3code', 'year'])[col].mean())
    new_df = yearly.unstack()
    return new_df

"""
Explanation: Create function to unstack
Create a list of the countries with the highest average HIV incidence from 1990 to 2013.
The subset_unstack function takes a column and dataframe, creates a temporary dataframe for each country, and concatenates the dataframes for each country together. Then unstacks to expand the dataframe by year.
End of explanation
"""


temp = highest_countries[-1]
oda1_df = undata[undata['countryname'] == temp]
oda1_df = oda1_df[['countryname', 'iso3code', 'year', oda_mill]]

oda1b_df = subset_unstack(oda_mill, oda1_df)
oda1b_df

"""
Explanation: ODA in millions US dollars
End of explanation
"""


oda2_df = undata[undata['countryname'] == temp]
oda2_df = oda2_df[['countryname', 'iso3code', 'year', oda_pc]]

oda2b_df = subset_unstack(oda_pc, oda2_df)
oda2b_df

"""
Explanation: ODA per capita
End of explanation
"""


oda3_df = undata[undata['countryname'] == temp]
oda3_df = oda3_df[['countryname', 'year', oda_pct]]

oda3b_df = subset_unstack(oda_pct, oda3_df)
oda3b_df

"""
Explanation: ODA as percentage of GNI (gross national income)
End of explanation
"""


byregion = undata.groupby(['year', 'mdgregions'])[hiv].mean()
byregion = byregion.unstack()
byregion.to_csv('hiv_byregion.csv')
byregion

byregion = byregion.transpose()
byregion

"""
Explanation: Explore precent change by region
End of explanation
"""


print 'max: %f \nmin: %f' % (byregion.ix[9].max(), byregion.ix[9].min())

ssafrica = byregion.ix[9]
change = ssafrica[[1996, 2013]]
change.pct_change()

"""
Explanation: Find percent change in sub-Saharan Africa from peak in 1996 to low in 2013
End of explanation
"""


# worldwide yearly mean of HIV incidence rate
world_means = undata.groupby(['year'])[hiv].mean()
print 'max: %f \nmin: %f' % (world_means.max(), world_means.min())

world_change = world_means[[1996, 2013]]
world_change.pct_change()

"""
Explanation: Find percent change worldwide from peak in 1996 to low in 2013
End of explanation
"""


