Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas : how to groupby time and column

Tags:

python

I have a list of sales products, and I would like to group the sales by product by month. I am using a dataframe, which index is a time index, and have a column product.

I tried :

df.groupby(['product', pd.TimeGrouper(freq='1M')])['sales'].sum()

but it give an error : 'TimeGrouper' object is not callable

I also tried

df.groupby(pd.TimeGrouper(freq='1M')).groupby('sales').sum()

but it gives an error : cannot access callable atttribute 'groupby' of 'dataframegroupby' objects.

Any ideas ?

PS : I can't copy paste code from my work to internet.

like image 983
Romain Jouin Avatar asked Feb 10 '23 02:02

Romain Jouin


1 Answers

This worked for me. I'm not sure why you're getting the TypeError...

df.groupby(['product', pd.TimeGrouper('1M')])['sales'].sum()
like image 103
Sebastian Avatar answered Feb 13 '23 22:02

Sebastian