How to add sequential counter column on groups using Pandas groupby

后端 未结 2 1134
执念已碎
执念已碎 2020-11-21 06:10

I feel like there is a better way than this:

import pandas as pd
df = pd.DataFrame(
    [[\'A\', \'X\', 3], [\'A\', \'X\', 5], [\'A\', \'Y\', 7], [\'A\', \'Y         


        
2条回答
  •  盖世英雄少女心
    2020-11-21 07:10

    use cumcount(), see docs here

    In [4]: df.groupby(['c1', 'c2']).cumcount()
    Out[4]: 
    0     0
    1     1
    2     0
    3     1
    4     0
    5     1
    6     2
    7     0
    8     0
    9     0
    10    1
    11    2
    dtype: int64
    

    If you want orderings starting at 1

    In [5]: df.groupby(['c1', 'c2']).cumcount()+1
    Out[5]: 
    0     1
    1     2
    2     1
    3     2
    4     1
    5     2
    6     3
    7     1
    8     1
    9     1
    10    2
    11    3
    dtype: int64
    

提交回复
热议问题