Efficient pandas/numpy function for time since change

前端 未结 2 1216
迷失自我
迷失自我 2021-01-21 04:59

Given a Series , I would like to efficiently compute how many observations have passed since there was a change. Here is a simple example:

ser = pd.         


        
2条回答
  •  轮回少年
    2021-01-21 05:46

    You can use groupby.cumcount:

    ser.groupby(ser).cumcount()
    
    #0    0
    #1    1
    #2    2
    #3    3
    #4    0
    #5    1
    #6    2
    #7    0
    #8    0
    #dtype: int64
    

提交回复
热议问题