计算用户会话数,定义为间隔

Count the number of user sessions, defined as intervals

我有一个用户会话数据集,加载到 Pandas DataFrame 中:

SessionID, UserID, Logon_time, Logoff_time
Adx1YiRyvOFApQiniyPWYPo,AbO6vW58ta1Bgrqs.RA0uHg,2016-01-05 07:46:56.180,2016-01-05 08:04:36.057
AfjMzw8In8RDqK6jIfItZPs,Ae8qOxLzozJHrC2pr2dOw88,2016-01-04 14:48:47.183,2016-01-04 14:53:30.210
AYIdSJYsRw5PptkFfEOXPa0,AX3Xy8dRDBRAlhyy3YaWw6U,2016-01-04 11:06:37.040,2016-01-04 16:34:38.770
Ac.WXBBSl75KqEuBmNljYPE,Ae8qOxLzozJHrC2pr2dOw88,2016-01-04 10:58:04.227,2016-01-04 11:21:10.520
AekXRDR3mBBDh49IIN2HdU8,Ae8qOxLzozJHrC2pr2dOw88,2016-01-04 10:16:08.040,2016-01-04 10:34:20.523
AVvL3VSWSq5Fr.f4733X.T4,AX3Xy8dRDBRAlhyy3YaWw6U,2016-01-04 09:19:29.773,2016-01-04 09:40:25.157

我想做的是将此数据转换为具有两列的 DataFrame:

我可以为单个时间戳执行此操作,方法是将日期时间范围转换为 Interval,然后检查给定时间戳落入该时间间隔的行数。

但是,如果我想这样做一两年,分辨率为分钟或小时,我将在一年内完成 8760 次循环(以小时计)……这可能不会成为交易破坏者,但我想知道是否有人有任何其他(可能更优雅)的建议或想法。

IIUC,我们可以这样做:

df.apply(lambda x: pd.Series([1] * len(pd.date_range(x.Logon_time, x.Logoff_time, freq='T')), 
         index=pd.date_range(x.Logon_time, x.Logoff_time, freq='T')), axis=1)\
  .stack().reset_index(level=0, drop=True).resample('T').count()

输出(头):

2016-01-04 09:19:00    1
2016-01-04 09:20:00    1
2016-01-04 09:21:00    1
2016-01-04 09:22:00    1
2016-01-04 09:23:00    1
Freq: T, dtype: int64

使用Pandas可视化检查所有数据:

df.apply(lambda x: pd.Series([1] * len(pd.date_range(x.Logon_time, x.Logoff_time, freq='T')),
                             index=pd.date_range(x.Logon_time, x.Logoff_time, freq='T')), axis=1)\
  .stack().reset_index(level=0, drop=True).resample('T').count().plot()

我最终使用了与 Scott 的答案略有不同的解决方案,但他的方法是关键,因为观察(记录)的数量相对较少,而另一方面,时间元素的数量(例如秒,取决于所需的分辨率)考虑到第一次和最后一次观察之间经过的时间要大得多。

但是,我首先将所有生成的日期范围(系列)收集到一个列表中,然后在第二个单独的步骤中将它们连接起来,这比使用 apply() 连续修改原始 Dataframe 更快。

# Expand the datetime range, creating records according to the given resolution (e.g. minutes).
# This creates a Series object for each session. All of those Series objects are then added to a list
# in order to concatenate them in 1 go, which is more efficient.
sessions=[]

for key, cols in df_sessions.iterrows():
    sess = pd.Series(data=pd.date_range(start=cols['logon'].floor('T'),
                                        end=cols['logoff'].ceil('T'),
                                        freq='T'),
                     name='sess_dt')
    sessions.append(sess)

# Concatenate all Series objects and convert to a DataFrame
df_sessions_2 = pd.DataFrame(pd.Series().append(sessions, ignore_index=True), columns=['ref_dt'])

# Add a counter which we can use to aggregate
df_sessions_2['sess_cnt'] = 1

# Aggregate according to the datetime
df_sessions_2 = df_sessions_2.groupby('ref_dt').sum()

然后绘图只需要一个额外的语句:

df_sessions_2.plot()