如何使用 pandas groupby 和聚合来计算特定事件为特定 ID 花费的总时间?
How to use pandas groupby with aggregation for counting total time taken by a particular event for a particular ID?
示例输入:
ID Timestamp Event
1 1 2010-05-31 13:43:45 a
2 1 2010-05-31 13:44:25 a
3 1 2010-05-31 13:44:55 a
4 1 2010-05-31 13:45:35 b
5 1 2010-05-31 13:47:05 b
6 1 2010-05-31 13:47:45 a
7 1 2010-05-31 13:49:45 c
8 1 2010-05-31 13:50:45 b
9 1 2010-06-01 09:20:00 c
10 1 2010-06-01 09:22:00 c
11 1 2010-06-01 09:23:20 c
12 1 2010-06-01 09:24:00 a
13 1 2010-06-01 09:24:15 a
14 1 2010-06-01 09:25:00 b
15 1 2010-06-01 09:25:42 b
16 2 2010-05-31 11:25:38 a
17 2 2010-05-31 11:26:00 c
18 2 2010-05-31 11:26:45 a
19 2 2010-05-31 11:27:10 a
20 2 2010-05-31 11:27:37 a
21 2 2010-05-31 11:29:35 b
22 2 2010-05-31 11:30:25 b
. . . .
. . . .
. . . .
这是我目前正在做的事情的伪代码,但它不能正常工作
# Right now I'm looping through the dataframe, using this pseudocode:
for row in df.itertuples():
check for ID match: (say, 1 == 1)
check for event match: (say a == a)
calculate time duration,
check for < 3600
store in a dictionary of the events ('a', 'b',etc = keys), cumulative
store this events dictionary for the ID
else Increment session accordingly
else
store the previous occurrence time of the previous event,
use it for the next occurrence of the event, using a dictionary;
At the same time, find the current event's duration w.r.t.
the time stored at the previous changing of the event(in the above line),
again check for < 3600
store in a dictionary of the events ('a', 'b',etc = keys), cumulative
store this events dictionary for the ID
else Increment session accordingly
else
reset events dictionary to zero values
reset the event change dictionary to zero values
这是一个示例输出,但与我想要做的类似。
ID a_time b_time c_time Session
1 1 120 200 100 2
2 2 235 340 145 3
. . . . . .
. . . . . .
Actually there are 7 unique events in all, I've just mentioned 3
to keep it less complicated.
{基本上“a_time”= 当前 ID 事件“a”发生的所有时间总和(如果该事件的持续时间不超过 3600 秒;否则增加会话相应地计算 session+time-duration/3600,或者仅当日期也发生变化时才加 1)
‘’ = 相似条目}
希望:
是否有更多的 pythonic 或矢量化方法来执行此操作?喜欢使用
df.groupby(['ID'])['Timestamp'].diff()
我用了很多这个版本,但它对我不起作用,因为我得到 "no aggregation function available for pd.groupby"
如果我不需要使用时间日志,那么我只需要使用命令
df.groupby(['ID'])['Event'].value_counts().unstack().fillna(0)
获取输出,即每个 ID 的每个唯一事件的计数。
我获得的输出,使用伪代码方法只是重复第一行(这里只是一些随机数,未计算)如下所示,我也看不到所有事件,只有三个:
ID a_time b_time c_time
1 1 120 200 100
如上部分所述,将 groupby 与 ['Timestamp'].diff() 一起使用会给我错误 "no aggregation function available for pd.groupby". 我什至使用 sort_values,应用但我总是得到这个 相同的错误(刚刚在上面的行中提到)。
我自己解决了。我发现我可以使用这个:
df.at[index, col] = df.at[index, col] + duration
基本上,由于数据框可以可视化为矩阵,因此我仍然可以通过它的单元格访问数据框,所以我设法解决了它。
示例输入:
ID Timestamp Event
1 1 2010-05-31 13:43:45 a
2 1 2010-05-31 13:44:25 a
3 1 2010-05-31 13:44:55 a
4 1 2010-05-31 13:45:35 b
5 1 2010-05-31 13:47:05 b
6 1 2010-05-31 13:47:45 a
7 1 2010-05-31 13:49:45 c
8 1 2010-05-31 13:50:45 b
9 1 2010-06-01 09:20:00 c
10 1 2010-06-01 09:22:00 c
11 1 2010-06-01 09:23:20 c
12 1 2010-06-01 09:24:00 a
13 1 2010-06-01 09:24:15 a
14 1 2010-06-01 09:25:00 b
15 1 2010-06-01 09:25:42 b
16 2 2010-05-31 11:25:38 a
17 2 2010-05-31 11:26:00 c
18 2 2010-05-31 11:26:45 a
19 2 2010-05-31 11:27:10 a
20 2 2010-05-31 11:27:37 a
21 2 2010-05-31 11:29:35 b
22 2 2010-05-31 11:30:25 b
. . . .
. . . .
. . . .
这是我目前正在做的事情的伪代码,但它不能正常工作
# Right now I'm looping through the dataframe, using this pseudocode:
for row in df.itertuples():
check for ID match: (say, 1 == 1)
check for event match: (say a == a)
calculate time duration,
check for < 3600
store in a dictionary of the events ('a', 'b',etc = keys), cumulative
store this events dictionary for the ID
else Increment session accordingly
else
store the previous occurrence time of the previous event,
use it for the next occurrence of the event, using a dictionary;
At the same time, find the current event's duration w.r.t.
the time stored at the previous changing of the event(in the above line),
again check for < 3600
store in a dictionary of the events ('a', 'b',etc = keys), cumulative
store this events dictionary for the ID
else Increment session accordingly
else
reset events dictionary to zero values
reset the event change dictionary to zero values
这是一个示例输出,但与我想要做的类似。
ID a_time b_time c_time Session
1 1 120 200 100 2
2 2 235 340 145 3
. . . . . .
. . . . . .
Actually there are 7 unique events in all, I've just mentioned 3
to keep it less complicated.
{基本上“a_time”= 当前 ID 事件“a”发生的所有时间总和(如果该事件的持续时间不超过 3600 秒;否则增加会话相应地计算 session+time-duration/3600,或者仅当日期也发生变化时才加 1)
‘’ = 相似条目}
希望: 是否有更多的 pythonic 或矢量化方法来执行此操作?喜欢使用
df.groupby(['ID'])['Timestamp'].diff()
我用了很多这个版本,但它对我不起作用,因为我得到 "no aggregation function available for pd.groupby"
如果我不需要使用时间日志,那么我只需要使用命令
df.groupby(['ID'])['Event'].value_counts().unstack().fillna(0)
获取输出,即每个 ID 的每个唯一事件的计数。
我获得的输出,使用伪代码方法只是重复第一行(这里只是一些随机数,未计算)如下所示,我也看不到所有事件,只有三个:
ID a_time b_time c_time
1 1 120 200 100
如上部分所述,将 groupby 与 ['Timestamp'].diff() 一起使用会给我错误 "no aggregation function available for pd.groupby". 我什至使用 sort_values,应用但我总是得到这个 相同的错误(刚刚在上面的行中提到)。
我自己解决了。我发现我可以使用这个:
df.at[index, col] = df.at[index, col] + duration
基本上,由于数据框可以可视化为矩阵,因此我仍然可以通过它的单元格访问数据框,所以我设法解决了它。