Python pandas rank/sort based on another column that differs for each input

前端 未结 2 1031
故里飘歌
故里飘歌 2021-01-23 23:00

I would like to come up with the 4th column below based on the first three:

user    job  time  Rank
A   print   1559   2
A   print   1540   2
A   edit    1520           


        
2条回答
  •  忘了有多久
    2021-01-23 23:37

    First, assign a new column which contains the minimum time for user-job pairs:

    df['min_time'] = df.groupby(['user', 'job'])['time'].transform('min')
    

    Then group by each user and rank them:

    df.groupby('user')['min_time'].rank(method='dense').astype(int)
    Out: 
    0    2
    1    2
    2    1
    3    1
    4    3
    5    2
    6    2
    7    2
    8    1
    9    1
    Name: min_time, dtype: int64
    

提交回复
热议问题