How to add sequential counter column on groups using Pandas groupby

后端 未结 2 1133
执念已碎
执念已碎 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 06:52

    This might be useful

    df = df.sort_values(['userID', 'date'])
    grp = df.groupby('userID')['ItemID'].aggregate(lambda x: '->'.join(tuple(x))).reset_index()
    print(grp)
    

    it will create a sequence like this

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题