同月累计金额

Same month cumulative sum

当日期列中的“月份”相同时,获得数据值累计总和的最佳方法是什么?这可以使用 resample() + sum() 来完成吗?

我想我可以在 python 中使用现成的东西,而不是创建自定义函数。

想要:

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 

谢谢!

对于你的情况,这可以帮助你

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

输出:

      Value
Date       
Apr       4
Jan       3
Mar       8

我更喜欢 index.month_name 更直接的方法:

df.groupby(df.index.month_name()).sum()