groupby counter of rows

前端 未结 2 1699
情书的邮戳
情书的邮戳 2021-01-24 02:52

I am trying to create a new variable which counts how many times had been seen the same id over time.

Need to pass from this dataframe

   id     clae6  y         


        
2条回答
  •  离开以前
    2021-01-24 03:22

    By using cumcount

    df.groupby('id').cumcount().add(1)
    Out[1574]: 
    0     1
    1     2
    2     3
    3     4
    4     5
    5     6
    6     1
    7     2
    8     3
    9     4
    10    5
    11    1
    12    2
    13    3
    14    4
    dtype: int64
    

提交回复
热议问题