I have a table of users with login date and time stamps.
I am attempting to calculate the difference between each user’s two most recent logins.
For example (df):
User, logints
34, 2024-07-10 07:49:11.773
34, 2024-07-10 07:52:11.606
34, 2024-07-11 08:49:11.947
34, 2024-07-11 09:49:11.758
34, 2024-07-12 09:46:11.758
37, 2024-07-10 08:46:11.587
37, 2024-07-10 08:49:11.356
37, 2024-07-09 08:49:11.744
38, 2024-07-10 08:55:11.742
Desired Result:
User, logindelta
34, 1 day
37, 3 minutes
38, na
Here’s what I have tried:
top_two_userlogins = df.groupby('User')['logints'].nlargest(2).diff()
The issue is the diff does not consistently be calculating within the groupby.
I’ve also tried pivoting into columns for the calculation unsuccessfully. It seems as though there should be an operation I can run on the group.