# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

from datetime import datetime
import os
import pandas as pd

df = pd.DataFrame({'cust_id' : ['101', '102', '103','101','102', '101', '101'],
                   'login_ts': ['2:01', '2:02', '2:03','2:05','2:10', '2:10','2:11']})
df['login_ts'] = pd.to_datetime(df.login_ts)

df

df = df.sort_values(by=['login_ts'])

df.iloc[1]

# +
n , _ = df.shape
dic = {}
order = []
index, i = 0, 0

while i < n:
    cst_id, login_time = df.iloc[i]
    i + = 1
    if cst_id in dic.keys() and (login_time- dic[cst_id][0]).seconds/60<5:
        order.append(dic[cst_id][1])
        dic[cst_id] = (login_time, dic[cst_id][1])
    else:
        index + =1
        dic[cst_id]= (login_time, index)
        order.append(index)
# -

dic

df['order']=order

df



StarcoderdataJupyterScriptsDedupFiltered