Ranking Rows of Pandas DataFrame
To rank the rows of Pandas DataFrame we can use the
DataFrame.rank() method which returns a rank of every respective index of a series passed. The rank is returned on the basis of position after sorting.
Example #1 :
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Here we will create a DataFrame of movies and rank them based on their ratings.
Let’s take an example of marks scored by 4 students. We will rank the students based on the highest mark they have scored.
Notice here that we have Raj and Arjun getting 90 marks each and hence they get ranked 2.5 (average of 2nd and 3rd rank i.e the two ranks they share). This can be seen for other marks in the table as well.