我有以下排序的数据框:
import pandas as pd
hits = {'id': ['A','A','A','A','B','B','C','C'],
'datetime': ['2010-01-02 03:00:00','2010-01-02 03:05:10','2010-01-02 03:51:35','2010-01-02 04:40:20',
'2010-01-02 03:29:10','2010-01-02 03:29:15','2010-01-02 03:45:20','2010-01-02 06:10:05'],
'value': [1,2,2,1,1,3,2,4]
}
df = pd.DataFrame(hits, columns = ['id', 'datetime','value'])
df['datetime'] = pd.to_datetime(df['datetime'], format='%Y-%m-%d %H:%M:%S')
print (df)
id datetime value
0 A 2010-01-02 03:00:00 1
1 A 2010-01-02 03:05:10 2
2 A 2010-01-02 03:51:35 2
3 A 2010-01-02 04:40:20 1
4 B 2010-01-02 03:29:10 1
5 B 2010-01-02 03:29:15 3
6 C 2010-01-02 03:45:20 2
7 C 2010-01-02 06:10:05 4
该列id允许我区分独特的用户,但我想向前迈出一步,能够按会话对点击进行分组。一次会话定义为不活动时间不超过 30 分钟的所有用户活动。
在我的 DataFrame 中,所需的输出应该是:
id datetime value session
0 A 2010-01-02 03:00:00 1 1
1 A 2010-01-02 03:05:10 2 1
2 A 2010-01-02 03:51:35 2 2
3 A 2010-01-02 04:40:20 1 3
4 B 2010-01-02 03:29:10 1 1
5 B 2010-01-02 03:29:15 3 1
6 C 2010-01-02 03:45:20 2 1
7 C 2010-01-02 06:10:05 4 2
在中SQL,我将首先使用lag来计算点击次数之间的差异partition by id order by datetime asc,然后在新的查询中,我sum(case when diff > 30min then 1 else 0 end)也将按 id 进行分区。
Pandas 有类似的东西吗?
牧羊人nacy
慕村225694
相关分类