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

前端 未结 2 1000
故里飘歌
故里飘歌 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:47

    Or you can using

    df1=df1.sort_values(['user','time'],ascending=[True,True])
    df1['Rank']=df1.job!=df1.job.shift().fillna('edit')
    df1.Rank=df1.groupby('user').Rank.cumsum()+1
    
    
      user      job  time  Rank
    0    A    print  1559   2.0
    1    A    print  1540   2.0
    2    A     edit  1520   1.0
    3    A     edit  1523   1.0
    4    A  deliver  9717   3.0
    5    B     edit  1717   2.0
    6    B     edit  1716   2.0
    7    B     edit  1715   2.0
    8    B  deliver  1527   1.0
    9    B  deliver  1524   1.0
    

提交回复
热议问题