jjgasse
jjgasse

Reputation: 329

Groupby more than1 columns and then average over another column

I have the follow dataset:

     cod           date              value 
0   1O8        2015-01-01 00:00:00    2.1
1   1O8        2015-01-01 01:00:00    2.3
2   1O8        2015-01-01 02:00:00    3.5
3   1O8        2015-01-01 03:00:00    4.5
4   1O8        2015-01-01 04:00:00    4.4
5   1O8        2015-01-01 05:00:00    3.2
6   1O9        2015-01-01 00:00:00    1.4
7   1O9        2015-01-01 01:00:00    8.6
8   1O9        2015-01-01 02:00:00    3.3
10  1O9        2015-01-01 03:00:00    1.5
11  1O9        2015-01-01 04:00:00    2.4
12  1O9        2015-01-01 05:00:00    7.2

I want to aggregate by cod and date(month) and do an average of the value, like this:

                  value     
cod  date
1O8  2015-01-01    3.3
1O9  2015-01-01    4.9

My data have the follow type: dtypes: object(1), datetime64[ns](1), float64(1)

I try to use .groupby() function to aggegrate:

df.groupby(['cod', 'date', 'value']).size().reset_index().groupby('value').mean()

But did'nt produce the correct result

Upvotes: 1

Views: 23

Answers (1)

Maarten Fabré
Maarten Fabré

Reputation: 7058

using a Grouper

df.groupby(["cod", pd.Grouper(key="date", freq="MS")]).mean()

Extra info on pbpython.com

Upvotes: 1

Related Questions