I used the code below to create the following data frame:
df = pd.DataFrame({'date':[['2024-02-22 03:42:06.065'],['2024-02-22 03:47:29.152'], ['2024-02-21 19:37:05.142'], ['2024-02-21 19:40:13.851'], ['2024-02-21 19:41:21.388'], ['2024-02-21 19:47:29.828'], ['2024-02-21 19:48:10.684'],['2024-02-22 02:59:36.786']]})
id | Date |
---|---|
AB1234 | [2024-02-22 03:42:06.065] |
AB1234 | [2024-02-22 03:46:29.152] |
CD3456 | [2024-02-21 19:37:05.142] |
CD3456 | [2024-02-21 19:40:13.851] |
CD3456 | [2024-02-21 19:41:21.388] |
GH3447 | [2024-02-21 19:47:29.828] |
GH3447 | [2024-02-21 19:48:10.684] |
GH3447 | [2024-02-22 02:59:36.786] |
I need to check if the difference between the dates in 2 (or more) consecutive rows that belong to the same id is <= 5 minutes. (So, if 4 rows belong to the same id, we compare that the diff between row1 and row2, between row2 and row3, and row3 and row4 are <=5 minutes). If yes, group those rows together (below the 'count' column). If not, put them in a different group (as shown in the 'count' column). The purpose is to check if the difference between two consecutive dates that belong to the same id is <= 5 minutes. So, the output should be something like the below:
id | Date | count |
---|---|---|
AB1234 | [2024-02-22 03:42:06.065] | 1 |
AB1234 | [2024-02-22 03:46:29.152] | 1 |
CD3456 | [2024-02-21 19:37:05.142] | 2 |
CD3456 | [2024-02-21 19:40:13.851] | 2 |
CD3456 | [2024-02-21 19:41:21.388] | 2 |
GH3447 | [2024-02-21 19:47:29.828] | 3 |
GH3447 | [2024-02-21 19:48:10.684] | 3 |
GH3447 | [2024-02-22 02:59:36.786] | 4 |
Any idea how to do this?