Quantcast
Viewing all articles
Browse latest Browse all 14389

How to filter users that had an event between two other events in pandas?

I have a table that looks like the following:

user_ideventtimestamp
1launch2023-09-15 14:31:44
1buy2023-09-15 14:31:49
1cancel2023-09-15 14:31:50
1add2023-09-15 14:31:52
2add2023-09-16 14:31:40
2cancel2023-09-16 14:31:44
2launch2023-09-16 14:31:49
2buy2023-09-16 14:31:50
2buy2023-09-16 14:31:52
3buy2023-09-17 14:31:44
3cancel2023-09-17 14:31:49
3remove2023-09-17 14:31:50
3add2023-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.


Viewing all articles
Browse latest Browse all 14389

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>