group by rank continuous date by pandas

后端 未结 2 759
萌比男神i
萌比男神i 2021-01-25 16:12

I refer this post . But My goal is something different.

Example

ID    TIME
01    2018-07-11
01    2018-07-12
01    2018-07-13
01    2018-0         


        
2条回答
  •  一整个雨季
    2021-01-25 16:38

    First we check the difference between the dates, which are > 1 day. Then we groupby on ID and the cumsum of these differences and cumulative count each group`

    # df['TIME'] = pd.to_datetime(df['TIME'])
    s = df['TIME'].diff().fillna(pd.Timedelta(days=1)).ne(pd.Timedelta(days=1))
    df['RANK'] = s.groupby([df['ID'], s.cumsum()]).cumcount().add(1)
    
    
       ID       TIME  RANK
    0   1 2018-07-11     1
    1   1 2018-07-12     2
    2   1 2018-07-13     3
    3   1 2018-07-15     1
    4   1 2018-07-16     2
    5   1 2018-07-17     3
    6   2 2019-09-11     1
    7   2 2019-09-12     2
    8   2 2019-09-15     1
    9   2 2019-09-16     2
    

提交回复
热议问题