Reputation: 6485
I have a dataframe and would like to assign a rank to each row in a group. For example,
A,B,C,D,E
---------
1,2,5,3,5
2,4,5,4,3
1,2,3,4,5
2,4,4,5,6
3,5,6,7,7
So I'd like to group by columns A
and B
and then assign a rank based on the value of column C
but I'd like to keep the D
and E
as well. If I do a group by, I have to drop the D
and E
. What's the easiest way to do this?
Upvotes: 0
Views: 152
Reputation: 5470
I would do the following:
df['rank'] = df.groupby(['A', 'B'])['C'].transform(lambda x: x.rank())
Upvotes: 1