简体   繁体   English

Append 来自 pandas 滚动 window 的唯一组 id

[英]Append unique group ids from pandas rolling window

I have a dataframe with timestamps, which I have used DataFrame.rolling() to find occurrences within a small time window (10 seconds), following the accepted answer to this question .我有一个带有时间戳的 dataframe,我使用了 DataFrame.rolling()在很短的时间内查找出现的事件 window(10 秒),遵循这个问题的接受答案

However, I would like to add a new column with unique group ids for all the rows which occur within the same time window.但是,我想为在同一时间 window 内出现的所有行添加一个具有唯一组 ID 的新列。

After running the accepted answer, my dataframe looks like this:运行接受的答案后,我的 dataframe 如下所示:

timestamp                user_id    count
2021-01-08 10:00:01      1          1
2021-01-08 10:00:02      2          2
2021-01-08 10:00:03      3          3
2021-01-08 10:00:09      1          4
2021-01-08 11:00:01      1          1
2021-01-08 11:00:02      7          2
2021-01-08 11:00:11      3          1

My desired output would be something like this:我想要的 output 是这样的:

timestamp                user_id    count    window_group
2021-01-08 10:00:01      1          1        1
2021-01-08 10:00:02      2          2        1
2021-01-08 10:00:03      3          3        1
2021-01-08 10:00:09      1          4        1
2021-01-08 11:00:01      1          1        2
2021-01-08 11:00:02      7          2        2
2021-01-08 11:00:11      3          1        3

You can identify the reset in number with diff and boolean not ( ~ ), then cumsum :您可以使用diff和 boolean 而不是( ~ )来识别重置数量,然后cumsum

df['window_group'] = (~df['count'].diff().gt(0)).cumsum()

output: output:

             timestamp  user_id  count  window_group
0  2021-01-08 10:00:01        1      1             1
1  2021-01-08 10:00:02        2      2             1
2  2021-01-08 10:00:03        3      3             1
3  2021-01-08 10:00:09        1      4             1
4  2021-01-08 11:00:01        1      1             2
5  2021-01-08 11:00:02        7      2             2
6  2021-01-08 11:00:11        3      1             3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM