group by rank continuous month by pandas

后端 未结 1 1462
说谎
说谎 2021-01-27 18:10

This is something different by this postenter link description here.

Example

ID    TIME
01    2018-07-01
01    2018-08-01
01    2018-09-01         


        
相关标签:
1条回答
  • 2021-01-27 18:44

    You can convert datetimes to months periods by Series.dt.to_period and then compare by MonthEnd difference of values by DataFrameGroupBy.diff with cumulative sums and last is used GroupBy.cumcount:

    df['TIME'] = pd.to_datetime(df['TIME']).dt.to_period('M')
    new = df.groupby('ID', group_keys=False)['TIME'].diff().ne(pd.offsets.MonthEnd()).cumsum()
    df['rank'] = df.groupby(['ID',new]).cumcount().add(1)
    print (df)
      ID     TIME  RANK  rank
    0   1  2018-07     1     1
    1   1  2018-08     2     2
    2   1  2018-09     3     3
    3   1  2018-11     1     1
    4   1  2018-12     2     2
    5   1  2019-01     3     3
    6   2  2019-01     1     1
    7   2  2019-02     2     2
    8   2  2019-12     1     1
    9   2  2020-01     2     2
    
    0 讨论(0)
提交回复
热议问题