Python - Ranking Rows of Pandas DataFrame
Last updated Dec 31, 2021Python is a powerful language for data analysis because it has a great community of programs that work with data. Pandas is one of these packages, making it much easier to import and analyze data.
Pandas Dataframe.rank() method is used to return the rank of each index in the series given. The ranking is performed based on sorting.
How to Rank Rows of Pandas DataFrame
To rank the rows of the Pandas DataFrame, we use the following syntax
DataFrame.rank(axis=0, na_option=’keep’, numeric_only=None, method=’average’, pct=False, ascending=True) |
Example
Now let’s create a sample DataFrame of different variables and rank them based on their ratings
import pandas as pd games = {'Name': ['Call Of Duty', 'Total Overdose', 'GTA 3', 'Bully'], 'Play Time(in hours)': ['45', '46', '52', '22'], 'Rate': ['Better than Average', 'Good', 'Best', 'Average']} df = pd.DataFrame(games) df['ranking'] = df['Play Time(in hours)'].rank(ascending=0) print(df) print("Hello World!");
|
Output:
![]() |
Article Contributed By :
|
|
|
|
691 Views |