AndryJ
AndryJ

Reputation: 3

pandas ranking based on a value

Hi I need to ranking the group of event that repeat in pandas df. If met value 'retry' the next row's rank will be increased. The perfect output will be:

event rank
init 0.
call 0.
retry 0.
jini 1.
init 1.
call 1.
retry 1.
jini 2.

Upvotes: 0

Views: 48

Answers (1)

fsl
fsl

Reputation: 3280

That doesn't quite seem like a rank, but you could do it like that:

# Shifting so the count increases after hitting a "retry"
df['rank'] = (df.event == 'retry').shift().cumsum().fillna(0)                                                             

Output:

   event  rank
0   init   0.0
1   call   0.0
2  retry   0.0
3   jini   1.0
4   init   1.0
5   call   1.0
6  retry   1.0
7   jini   2.0

Upvotes: 1

Related Questions