原始数据集
userId createDate grade
0 2016-05-08 22:00:49.673 2
0 2016-07-23 12:37:11.570 7
0 2017-01-03 12:05:33.060 7
1009 2016-06-27 09:28:19.677 5
1009 2016-07-23 12:37:11.570 8
1009 2017-01-03 12:05:33.060 9
1009 2017-02-08 16:17:17.547 4
2011 2016-11-03 14:30:25.390 6
2011 2016-12-15 21:06:14.730 11
2011 2017-01-04 20:22:31.423 2
2011 2017-08-08 16:17:17.547 7
我想要从createDate开始的6个月的回溯窗口的每个用户的滚动等级总和,即(从创建日期起6个月内所有等级的总和) 期望:
^{pr2}$我当前的尝试不正确:
df.groupby(['userId','createDate'])['grade'].mean().groupby([pd.Grouper(level='userId'),pd.TimeGrouper('6M', level='createDate', closed = 'left')]).cumsum()
结果如下:
userId createDate
0 2016-05-08 22:00:49.673 2
2016-07-23 12:37:11.570 9
2017-01-03 12:05:33.060 7
1009 2016-06-27 09:28:19.677 5
2016-07-23 12:37:11.570 13
2017-01-03 12:05:33.060 9
2017-02-08 16:17:17.547 13
2011 2016-11-03 14:30:25.390 6
2016-12-15 21:06:14.730 17
2017-01-04 20:22:31.423 19
2017-08-08 16:17:17.547 7
在
apply
内使用groupby
和rolling sum
,偏移量为180D
,而不是6个月,因为月中的天数往往每隔一个月发生变化。滚动窗口必须是一个常数,即编辑评论:
回顾6个月前的日子,需要整理一下。所以也许你需要
^{pr2}$sort_values
根据@coldspeed的评论进行编辑:
使用“应用”是一种过度使用,请先设置索引,然后使用滚动求和:
^{3}$时间安排:
相关问题 更多 >
编程相关推荐