python - 同月累计

标签 python pandas 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 

谢谢!

最佳答案

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

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

关于python - 同月累计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70444466/

相关文章:

python - Tornado -对象内存处置

pandas - 根据同一数据框中的其他行从数据框中删除行

c# - 用于运行总计的全局变量的替代方案

python - 使用参数启动 pytest fixture

python - Pyglet:找不到库 "GL"

python - Pandas 索引中值而不是值中值

mysql - MySQL中按日期和用户列分组的累积和

r - 在 R (dplyr) 中重置的条件运行计数(累计和)

python - 如何匹配 python 正则表达式中的零个或多个括号

python - 面向对象编程基础(python)