-1

I have a dataset as shown:

student_id     course_marks
1234           10
9887           30
9881           20
5634           40
5634           50
1234           60
1234           70

I want to sort them using course_marks, then rank them within their student_id

expected:

student_id     course_marks   rank
1234           10             1
1234           60             2
1234           70             3
5634           40             1
5634           50             2
9887           20             1
9887           30             2
floss
  • 2,603
  • 2
  • 20
  • 37
Cathy
  • 367
  • 1
  • 3
  • 16

1 Answers1

2
df['rank'] = df.groupby('student_id')['course_marks'].rank()
   student_id  course_marks  rank
0        1234            10   1.0
1        9887            30   1.0
2        9881            20   1.0
3        5634            40   1.0
4        5634            50   2.0
5        1234            60   2.0
6        1234            70   3.0

or, sorted:

   student_id  course_marks  rank
0        1234            10   1.0
5        1234            60   2.0
6        1234            70   3.0
3        5634            40   1.0
4        5634            50   2.0
2        9881            20   1.0
1        9887            30   1.0

(Note that you have 9881 and 9887 in your example data, and 9887 twice in your expected output.)

Brendan
  • 3,901
  • 15
  • 23