I have a dataframe of events by users, and I want to keep any events that have taken place within a given time (e.g. 2 hours) of each other, that are associated to the same user and the same store. Here is an example dataframe:
user_id | timestamp | store_id |
---|---|---|
user_1 | 2021-11-26T13:40:00.000Z | store_1 |
user_1 | 2021-11-26T12:20:00.000Z | store_1 |
user_1 | 2021-11-22T16:10:00.000Z | store_1 |
user_2 | 2021-11-19T22:00:00.000Z | store_2 |
user_2 | 2021-11-19T19:50:00.000Z | store_2 |
user_3 | 2021-11-28T06:10:00.000Z | store_1 |
user_4 | 2021-11-18T16:30:00.000Z | store_3 |
user_4 | 2021-11-18T16:20:00.000Z | store_2 |
Applying the filtering, the ouput dataframe should look like this:
user_id | timestamp | store_id |
---|---|---|
user_1 | 2021-11-26T13:40:00.000Z | store_1 |
user_1 | 2021-11-26T12:20:00.000Z | store_1 |
Because only the first two events by user_1
took place at the same store, by the same user, and within 2 hours of one another. I have been searching through stackoverflow questions, but nothing seems to fit this scenario. Any help would be really appreciated!
EDIT: Following Time difference between two event rows for each user in Pandas df, I am calculating the time difference between rows, grouped by user.