Home > Blockchain >  Filtering dataframe records given a date range
Filtering dataframe records given a date range

Time:08-26

My df fields are 'User ID' (string), 'Last Login' (%Y-%m-%d %H:%M:%S). I want to know the User IDs that haven't Logged in for the past 3 months. The 'Last Login' field contains 12 months of login history but only the last login per user. I need the User ID field to be preserved to compare against a list of User IDs that are in scope for my analysis.

I have tried a few variations of code using datetime but am struggling to get results.

CodePudding user response:

import time
import pandas as pd
import numpy as np

now = pd.to_datetime(time.ctime())
idx = (now - df['Last Login']) / np.timedelta64(1, 'M') <= 3
df2 = df[idx]

CodePudding user response:

Get the delta between today's date and last login, then apply a mask on the dataframe to get your final result.

import datetime as dt

current_date = dt.datetime.today()
df['Last Login Days'] = abs((df['Last Login'] - current_date).days)
absent_users_df = df[df['Last Login Days'] > 90]

  • Related