compare columns in pivot table and add result

Viewed 78

I'm using an open data csv about the population in Senegal coming from http://senegal.opendataforafrica.org/SNVS2015/vital-statistics-of-senegal-2015 . Imported that with pandas into a dataframe (shape 17568,7).

    region  regional-division   sex indicator                               Unit    Date    Value
0   Dakar   Total   Total       Populations (projection de 2008 à   2015)   Number  2008    2482294.0 
1   Dakar   Total   Total       Populations    (projection de 2008 à 2015)  Number  2009    2536959.0
2   Dakar   Total   Total       Populations (projection de 2008 à   2015)   Number  2010    2592191.0 
3   Dakar   Total   Total       Populations   (projection de 2008 à 2015)   Number  2011    2647751.0
4   Dakar   Total   Total       Populations (projection de 2008 à   2015)   Number  2012    2703203.0 
5   Dakar   Total   Total       Populations   (projection de 2008 à 2015)   Number  2013    2776787.0
6   Dakar   Total   Total       Populations (projection de 2008 à   2015)   Number  2014    2851556.0 
7   Dakar   Total   Total       Populations   (projection de 2008 à 2015)   Number  2015    2927422.0
8   Dakar   Total   Men         Populations (projection de 2008 à   2015)   Number  2008    1242463.0 
9   Dakar   Total   Men         Populations (projection   de 2008 à 2015)   Number  2009    1269764.0

Then did

total_population_condition = (population['sex'] == 'Total') & (population['regional-division'] == 'Total')
total_population = population[total_population_condition]

And on top of that

pivot_total_population = pd.pivot_table(total_population,values='Value',index=['region','sex'],columns='Date')

Pivot Table

And now the question: I want to find the 5 regions with the highest growth population between 2008 and 2015. And the 5 with the highest shrinkage. I was trying to access the pivot column with the "2008" values and the "2015" values and then dividing the latter into the former. And then adding the result to the dataframe. Didn't manage to. How would I do this?

Update: I just figured out how to ...

# compute growth first per region
pivot_total_population['growth'] = 
pivot_total_population.iloc[:,7]/pivot_total_population.iloc[:,0]

# then determine which are top 10 growing regions in terms of total population
pivot_total_population.sort_values(['growth'],ascending=False).head(10)

# then determine which are top 10 shrinking regions in terms of total population
pivot_total_population.sort_values(['growth'],ascending=True).head(10)
1 Answers

figured out the answer (thx gboffi for the process tip to a newbie ;-))

# compute growth first per region
pivot_total_population['growth'] = 
pivot_total_population.iloc[:,7]/pivot_total_population.iloc[:,0]

# then determine which are top 10 growing regions in terms of total population
pivot_total_population.sort_values(['growth'],ascending=False).head(10)

# then determine which are top 10 shrinking regions in terms of total population
pivot_total_population.sort_values(['growth'],ascending=True).head(10)
Related