pandas get average of a groupby : <p>I am trying to find the average monthly cost per user_id but i am only able to get average cost per user or monthly cost per user. </p>

<p>Because i group by user and month, there is no way to get the average of the second groupby (month) unless i transform the groupby output to something else.</p>

<p>This is my df:</p>

<pre><code>     df = { 'id' : pd.Series([1,1,1,1,2,2,2,2]),
            'cost' : pd.Series([10,20,30,40,50,60,70,80]),
            'mth': pd.Series([3,3,4,5,3,4,4,5])}

   cost  id  mth
0    10   1    3
1    20   1    3
2    30   1    4
3    40   1    5
4    50   2    3
5    60   2    4
6    70   2    4
7    80   2    5
</code></pre>

<p>I can get monthly sum but i want the average of the months for each user_id. </p>

<pre><code>df.groupby(['id','mth'])['cost'].sum()

id  mth
1   3       30
    4       30
    5       40
2   3       50
    4      130
    5       80
</code></pre>

<p>i want something like this:</p>

<pre><code>id average_monthly
1 (30+30+40)/3
2 (50+130+80)/3
</code></pre>
