Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 每小时统计汇总_Python_Python 3.x_Pandas - Fatal编程技术网

Python 每小时统计汇总

Python 每小时统计汇总,python,python-3.x,pandas,Python,Python 3.x,Pandas,我有以下数据帧: 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

我有以下数据帧:

                      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
我想将每小时的峰值数聚合到峰值计数列中,其中小时列是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
我不知道从哪里开始,也不知道如何解决这个问题。有什么建议吗?

使用和

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