I have a table that looks like the following:
user_id | event | timestamp |
---|---|---|
1 | launch | 2023-09-15 14:31:44 |
1 | buy | 2023-09-15 14:31:49 |
1 | cancel | 2023-09-15 14:31:50 |
1 | add | 2023-09-15 14:31:52 |
2 | add | 2023-09-16 14:31:40 |
2 | cancel | 2023-09-16 14:31:44 |
2 | launch | 2023-09-16 14:31:49 |
2 | buy | 2023-09-16 14:31:50 |
2 | buy | 2023-09-16 14:31:52 |
3 | buy | 2023-09-17 14:31:44 |
3 | cancel | 2023-09-17 14:31:49 |
3 | remove | 2023-09-17 14:31:50 |
3 | add | 2023-09-17 14:31:52 |
How do I filter users that had an event 'cancel' after event 'buy' and before event 'add' (with no regard to whether there were any other events between them or not)?
In other words, I need to get the following list of users:
1, 3
Thank you in advance
I tried to add columns 'buy_timestamp' and 'add_timestamp' and filter df likedf[df.event == 'cancel'].query('timestamp > 'buy_timestamp'& timestamp < 'add_timestamp')
But I wonder if there is a simpler way to do it.