我有以下数据框:
hour spike spike_count
date_time
2014-11-22 00:00:00 0 0 0
2014-11-22 01:00:00 1 1 0
2014-11-22 02:00:00 2 1 0
2014-11-22 03:00:00 3 1 0
2014-11-22 04:00:00 4 0 0
2014-11-22 05:00:00 5 0 0
2014-11-22 06:00:00 6 0 0
2014-11-22 07:00:00 7 0 0
2014-11-22 08:00:00 8 1 0
2014-11-22 09:00:00 9 0 0
2014-11-22 10:00:00 10 0 0
2014-11-22 11:00:00 11 1 0
2014-11-22 12:00:00 12 0 0
2014-11-22 13:00:00 13 0 0
2014-11-22 14:00:00 14 1 0
2014-11-22 15:00:00 15 0 0
2014-11-22 16:00:00 16 0 0
2014-11-22 17:00:00 17 0 0
2014-11-22 18:00:00 18 0 0
2014-11-22 19:00:00 19 1 0
2014-11-22 20:00:00 20 0 0
2014-11-22 21:00:00 21 0 0
2014-11-22 22:00:00 22 0 0
2014-11-22 23:00:00 23 1 0
我想将每小时的尖峰次数汇总到每小时的spin_count列中(其中hour列是24小时格式的小时)。所以我的预期输出如下所示:
hour spike spike_count
date_time
2014-11-22 00:00:00 0 0 0
2014-11-22 01:00:00 1 1 1
2014-11-22 02:00:00 2 1 2
2014-11-22 03:00:00 3 1 3
2014-11-22 04:00:00 4 0 0
2014-11-22 05:00:00 5 0 0
2014-11-22 06:00:00 6 0 0
2014-11-22 07:00:00 7 0 0
2014-11-22 08:00:00 8 1 4
2014-11-22 09:00:00 9 0 0
2014-11-22 10:00:00 10 0 0
2014-11-22 11:00:00 11 1 5
2014-11-22 12:00:00 12 0 0
2014-11-22 13:00:00 13 0 0
2014-11-22 14:00:00 14 1 6
2014-11-22 15:00:00 15 0 0
2014-11-22 16:00:00 16 0 0
2014-11-22 17:00:00 17 0 0
2014-11-22 18:00:00 18 0 0
2014-11-22 19:00:00 19 1 7
2014-11-22 20:00:00 20 0 0
2014-11-22 21:00:00 21 0 0
2014-11-22 22:00:00 22 0 0
2014-11-22 23:00:00 23 1 8
我不知道从哪里开始或如何解决这个问题。有什么建议吗?
答案 0 :(得分:4)
df['spike_count'] = df.spike.cumsum().mask(df.spike.eq(0), 0)
hour spike spike_count
date_time
2014-11-22 00:00:00 0 0 0
2014-11-22 01:00:00 1 1 1
2014-11-22 02:00:00 2 1 2
2014-11-22 03:00:00 3 1 3
2014-11-22 04:00:00 4 0 0
2014-11-22 05:00:00 5 0 0
2014-11-22 06:00:00 6 0 0
2014-11-22 07:00:00 7 0 0
2014-11-22 08:00:00 8 1 4
2014-11-22 09:00:00 9 0 0
2014-11-22 10:00:00 10 0 0
2014-11-22 11:00:00 11 1 5
2014-11-22 12:00:00 12 0 0
2014-11-22 13:00:00 13 0 0
2014-11-22 14:00:00 14 1 6
2014-11-22 15:00:00 15 0 0
2014-11-22 16:00:00 16 0 0
2014-11-22 17:00:00 17 0 0
2014-11-22 18:00:00 18 0 0
2014-11-22 19:00:00 19 1 7
2014-11-22 20:00:00 20 0 0
2014-11-22 21:00:00 21 0 0
2014-11-22 22:00:00 22 0 0
2014-11-22 23:00:00 23 1 8