How to obtain the size means of a grouping with Pandas?

1

Given a Pandas #, with data in a structure of this type:

import pandas as pd

raw_data = {
    'tipo': ['a', 'a', 'b', 'c', 'c', 'c', 'd'],
    'ano': [2000, 2000, 2000, 2001, 2001, 2001, 2001],
}

df = pd.DataFrame.from_dict(raw_data)

I want to get the averages of item numbers of different types per year.

Grouping with: df.groupby(['tipo', 'ano']).size() I get item numbers by type each year on a Pandas Series :

tipo  ano 
a     2000    2
b     2000    1
c     2001    3
d     2001    1
dtype: int64

I want to get the averages of these numbers per year, as in:

ano     media
2000    1.5
2001    2.0

In order to plot them, using Pandas.

I tried doing this with Pandas, but after a while trying to use the API and crashing I ended up giving up and did it with Python same, using a dictionary and calculating the averages "on hand."

Is there a simple way to do this using Pandas's own APIs and abstractions?

    
asked by anonymous 30.04.2014 / 15:07

1 answer

2

As per the response I just got in the English OS , the solution is to make another groupby specifying the parameter level :

df.groupby(['tipo', 'ano']).size().groupby(level=1).mean()

ano
2000    1.5
2001    2.0
dtype: float64
    
30.04.2014 / 16:07