现在的位置: 首页 > 问答 > 正文

How to count the number of the same instances in a column of a data frame in a rolling window

2017年09月14日 问答 ⁄ 共 496字 ⁄ 字号 暂无评论

I am trying to count a number of the same ID inside of each sliding window for this data:

                           ID  
DATE            
2017-05-17 15:49:51         s_2   
2017-05-17 15:49:52         s_5   
2017-05-17 15:49:55         s_2   
2017-05-17 15:49:56         s_3   
2017-05-17 15:49:58         s_5
2017-05-17 15:49:59         s_5

I am trying to count the number of same ID inside rolling window of size 3 which overlap each other. Answer should be like this:

DATE                    ID      s_2_count    s_3_count   s_5_count       
2017-05-17 15:49:51     s_2         2            0         1 
2017-05-17 15:49:52     s_5         1            1         1   
2017-05-17 15:49:55     s_2         1            1         1   
2017-05-17 15:49:56     s_3         0            1         2   
2017-05-17 15:49:58     s_5         NaN          NaN       NaN
2017-05-17 15:49:59     s_5         NaN          NaN       NaN

给我留言

您必须 [ 登录 ] 才能发表留言!

×