'Count number of events between two dynamic timestamps in pandas dataframe
I want to create a dataframe showing the number of total events within changing time intervals. In other words, I want to find the number of datapoints with time that fall between certain timestamps.
First, my dataframe looks like below:
| id | time | first_time | thirty_min_bf_first_time
| -- | ----------------- |--- --- --- --- -- |--- --- --- --- --- ---
| 121 | 22/01/01 00:02:00 | 22/01/01 00:32:00 | 22/01/01 00:02:00
| 121 | 22/01/01 01:22:34 | 22/01/01 00:32:00 | 22/01/01 00:02:00
| 245 | 22/04/01 12:00:00 | 22/04/01 12:00:00 | 22/04/01 11:30:00
| 245 | 22/04/01 13:23:34 | 22/04/01 12:00:00 | 22/04/01 11:30:00
I created 'first_time' and 'thirty_min_bf_first_time' columns based on 'time' for each events. what I want to create is a dataframe like below:
| id | total_events_btw_times |
| -- | -- ------------------- |
| 121 | 12 |
| 245 | 34 |
The new dataframe should be grouped by id and have a column showing total events between each time interval between df['thirty-min_bf_first_time'] and df['first_time'].
I have tried many things but didn't work out. Can anyone please help me out? Thanks!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
