How to add sequential counter column on groups using Pandas groupby

后端 未结 2 1178
执念已碎
执念已碎 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

提交回复
热议问题