lundi 30 août 2021

Is there a way to find the time elapsed from a datetime column in a dataframe based on a certain condition?

I have a data frame called actions. The columns are user_id, action_timestamp and action. The users are students who perform an action at a specific time given by the action_timestamp.

Which student has spent the longest uninterrupted session? (here a “session” is defined as a length of time where a student performs a series of actions with less than 10 minutes in between them; the “session” stop when the next action in the series has been performed 10 minutes or more after the last one, after which a new “session” starts)subset of df

The code I am trying:

new['diff']= new['action_timestamp'].diff().astype('timedelta64[m]')

for i in new['diff']:
    if not np.isnan:
        if (i <=10.0):
            new['session']= 1.0
        else:
            new['session']=0.0

new.loc[new['diff'] <= 10.0, 'session'] = 1.0 
new.loc[new['diff'] > 10.0, 'session'] = 0.0 

This does not prove to be the right answer. Any help would be much appreciated. Thank you.

Aucun commentaire:

Enregistrer un commentaire