So I am having some issues with finding cumulative sums using pandas.
I have a dataframe like this:
df = pd.DataFrame({
'Date': ['2018-04-01', '2018-04-01', '2018-04-01', '2018-05-01', '2018-05-01','2018-05-01','2018-04-01','2018-05-01'],
'Category': ['AA', 'AA', 'AA', 'AA', 'AA','AA','AA','AA'],
'Product': ['a', 'a', 'a', 'a', 'a','a','x','x'],
'Volumes': [10,30,40,50,60,10,1,2]})
Date Category Product Volumes
2018-04-01 AA a 10
2018-04-01 AA a 30
2018-04-01 AA a 40
2018-05-01 AA a 50
2018-05-01 AA a 60
2018-05-01 AA a 10
2018-04-01 AA x 1
2018-05-01 AA x 2
That is, some products are duplicated for the same date and some are unique.
I want to find the cumulative sum in this way:
df = pd.DataFrame({
'Date': ['2018-04-01', '2018-04-01', '2018-04-01', '2018-05-01', '2018-05-01','2018-05-01','2018-04-01','2018-05-01'],
'Category': ['AA', 'AA', 'AA', 'AA', 'AA','AA','AA','AA'],
'Product': ['a', 'a', 'a', 'a', 'a','a','x','x'],
'Volumes': [80,80,80,190,190,190,1,3]})
Date Category Product Volumes
2018-04-01 AA a 80
2018-04-01 AA a 80
2018-04-01 AA a 80
2018-05-01 AA a 200
2018-05-01 AA a 200
2018-05-01 AA a 200
2018-04-01 AA x 1
2018-05-01 AA x 3
Where 80 is the sum of the volumes for April, and 200 is the sum of the volumes for April and May.
I have tried a simple
df.groupby(['Category', 'Product'])['Volumes'].agg(['sum']).reset_index()```
But that doesnt give me the desired output.
Any suggestions?