Skip to content Skip to sidebar Skip to footer

How To Rank Rows By Id In Pandas Python

I have a Dataframe like this: id points1 points2 1 44 53 1 76 34 1 63 66 2 23 34 2 44 56 I

Solution 1:

You need to use ascending=False inside rank

df.join(df.groupby('id')['points1', 'points2'].rank(ascending=False).astype(int).add_suffix('_rank'))


+---+----+---------+---------+--------------+--------------+|| id | points1 | points2 | points1_rank | points2_rank |+---+----+---------+---------+--------------+--------------+|0|1|44|53|3|2||1|1|76|34|1|3||2|1|63|66|2|1||3|2|23|34|2|2||4|2|44|56|1|1|+---+----+---------+---------+--------------+--------------+

Solution 2:

Use join with remove reset_index and for change columns names add add_suffix:

features = ["points1","points2"]
df = df.join(df.groupby('id')[features].rank(ascending=False).add_suffix('_rank').astype(int))
print (df)
   id  points1  points2  points1_rank  points2_rank
0   1       44       53             3             2
1   1       76       34             1             3
2   1       63       66             2             1
3   2       23       34             2             2
4   2       44       56             1             1

Post a Comment for "How To Rank Rows By Id In Pandas Python"