using numpy to calculate mean
<p>I am trying to calculate the mean of GNP for each country from 2006 to 2015. But when I apply the aggregation with mean function, it will not calculate the mean from 2006 to 2015. Instead, it just display the values for each year. Pls tell me what went wrong? I am able to sort by country but the mean just wont work on the data.</p>

<pre><code>wb_indicator = 'NY.GNP.ATLS.CD'
start_year = 2006
end_year = 2015

df_ex = wb.download(indicator = wb_indicator,
                  country = ['all'],
                  start = start_year,
                  end = end_year)

df_ex1 = df_ex.reset_index()
df_ex1.groupby(['country']).agg({'NY.GNP.ATLS.CD': [np.mean]})

df_ex1.head(20)
</code></pre>

<p>Output:</p>

<blockquote>
  <p>country year    NY.GNP.ATLS.CD 0    Arab World  2015    2.767920e+12 1  Arab
  World 2014    2.897113e+12 2  Arab World  2013    2.832769e+12 3  Arab
  World 2012    2.590610e+12 4  Arab World  2011    2.190786e+12 5  Arab
  World 2010    2.055967e+12 6  Arab World  2009    1.932056e+12 7  Arab
  World 2008    1.858270e+12 8  Arab World  2007    1.547924e+12 9  Arab
  World 2006    1.312967e+12 10 Caribbean small states  2015    6.680302e+10
  11    Caribbean small states  2014    6.664219e+10</p>
</blockquote>

<p>This should work</p>

<pre><code>import pandas as pd
import wbdata as wb
import datetime


wb_indicator = 'NY.GNP.ATLS.CD'
data_date = (datetime.datetime(2006, 1, 1), datetime.datetime(2015, 1, 1))

data = wb.get_data(wb_indicator, data_date=data_date, pandas=True)
gnp_means = data.reset_index().groupby('country').mean()
</code></pre>

python python-2.7