Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same month cumulative sum

What is the best way to have a cumulative total of data values when the "month" in the date column is the same? Can this be done using resample() + sum()?

I'm thinking I can probably use something off the shelf in python instead of creating a custom function.

Want:

Input:
             Value 
Date                   
2015-01-01   1 
2014-01-01   2
2017-03-01   3 
2015-04-01   4 
2016-03-01   5 

Output:
             Value 
Month                   
January      3 
March        8
April        4 

Thanks!

like image 540
user1144251 Avatar asked Feb 14 '26 20:02

user1144251


2 Answers

I would prefer a more straightforward approach with index.month_name:

df.groupby(df.index.month_name()).sum()
like image 66
U12-Forward Avatar answered Feb 16 '26 23:02

U12-Forward


In your case , this can help you out

out = df.groupby(df.index.strftime('%b')).sum()

output:

      Value
Date       
Apr       4
Jan       3
Mar       8
like image 34
BENY Avatar answered Feb 16 '26 23:02

BENY