简体   繁体   中英

Hourly aggregation of counts pandas

I have the following dataframe:

                      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

I'd like to aggregate the number of spike into the spike_count column per hour (where the hour column is the hours on a 24 hr format). So my expected output would look like this:

                      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

I don't know where to start or how to go about solving this. Any suggestions?

Use cumsum and mask

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM