Pandas Dataframe rank() | Rank DataFrame Entries
Last Updated :
02 Feb, 2024
Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas DataFrame rank() method returns a rank of every respective entry (1 through n) along an axis of the DataFrame passed. The rank is returned based on position after sorting.
Example:
Python3
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 2, 3, 4],
'B': [5, 6, 7, 8, 9],
'C': [1, 1, 1, 1, 1]
})
df['A_rank'] = df['A'].rank()
print(df)
Output:
A B C A_rank
0 1 5 1 1.0
1 2 6 1 2.5
2 2 7 1 2.5
3 3 8 1 4.0
4 4 9 1 5.0
Syntax
Syntax: DataFrame.rank(axis=0, method='average', numeric_only=None, na_option='keep', ascending=True, pct=False)
Parameters:
- axis: 0 or ‘index’ for rows and 1 or ‘columns’ for Column. method: Takes a string input(‘average’, ‘min’, ‘max’, ‘first’, ‘dense’) which tells pandas what to do with same values. Default is average which means assign average of ranks to the similar values.
- numeric_only: Takes a boolean value and the rank function works on non-numeric value only if it's False.
- na_option: Takes 3 string input(‘keep’, ‘top’, ‘bottom’) to set position of Null values if any in the passed Series.
- ascending: Boolean value which ranks in ascending order if True. pct: Boolean value which ranks percentage wise if True.
Return type: Series with Rank of every index of caller series.
For link to CSV file Used in Code, click here.
Examples
Let's see some examples of how to check the rank of DataFrame data using dataframe.rank() method of the Pandas library.
Example 1
Ranking Column with Unique values In the following example, a new rank column is created which ranks the Name of every Player. All the values in the Name column are unique and hence there is no need to describe a method.
Python
# importing pandas package
import pandas as pd
# making data frame from csv file
data = pd.read_csv("nba.csv")
# creating a rank column and passing the returned rank series
data["Rank"] = data["Name"].rank()
# display
data
# sorting w.r.t name column
data.sort_values("Name", inplace = True)
# display after sorting w.r.t Name column
data
Output:
As shown in the image, a column 'rank' was created with the rank of every Name. After the sort_value function sorted the DataFrame for names, it can be seen that the rank was also sorted since those were ranking of Names only.
Before Sorting-

After Sorting-

Example 2:
Sorting Column with some similar values in the following example, DataFrame is first sorted for 'team name' and first the method is the default (i.e. average), and hence the rank of same Team players is average. After that min method is also used to see the output.
Python3
# importing pandas package
import pandas as pd
# making data frame from csv file
data = pd.read_csv("nba.csv")
# sorting w.r.t team name
data.sort_values("Team", inplace = True)
# creating a rank column and passing the returned rank series
# change method to 'min' to rank by minimum
data["Rank"] = data["Team"].rank(method ='average')
# display
data
Output:
With method='average'

With method='min'

Similar Reads
Pandas DataFrame.reset_index() In Pandas, reset_index() method is used to reset the index of a DataFrame. By default, it creates a new integer-based index starting from 0, making the DataFrame easier to work with in various scenarios, especially after performing operations like filtering, grouping or multi-level indexing. Example
3 min read
Python | Pandas Dataframe.at[ ] Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas at[] is used to return data in a dataframe at the passed location. The passed l
2 min read
Pandas DataFrame iterrows() Method iterrows() method in Pandas is a simple way to iterate over rows of a DataFrame. It returns an iterator that yields each row as a tuple containing the index and the row data (as a Pandas Series). This method is often used in scenarios where row-wise operations or transformations are required. Exampl
4 min read
Python | Pandas Series.iteritems() Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.iteritems() function iterates
2 min read
Pandas.to_datetime()-Python pandas.to_datetime() converts argument(s) to datetime. This function is essential for working with date and time data, especially when parsing strings or timestamps into Python's datetime64 format used in Pandas. For Example:Pythonimport pandas as pd d = ['2025-06-21', '2025-06-22'] res = pd.to_date
3 min read
Python | pandas.to_numeric method Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. pandas.to_numeric() is one of the general functions in Pandas which is used to convert
2 min read
Pandas DataFrame.to_string-Python Pandas is a powerful Python library for data manipulation, with DataFrame as its key two-dimensional, labeled data structure. It allows easy formatting and readable display of data. DataFrame.to_string() function in Pandas is specifically designed to render a DataFrame into a console-friendly tabula
5 min read
pandas.concat() function in Python pandas.concat() function concatenate two or more pandas objects like DataFrames or Series along a particular axis. It is especially useful when combining datasets either vertically (row-wise) or horizontally (column-wise). Example:Pythonimport pandas as pd df1 = pd.DataFrame({'A': ['A0', 'A1'], 'B':
3 min read
Python | Pandas dataframe.cov() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas dataframe.cov() is used to compute pairwise covariance of columns. If some of t
2 min read
Pandas DataFrame duplicated() Method - Python Pandas is widely used library in Python used for tasks like cleaning, analyzing and transforming data. One important part of cleaning data is identifying and handling duplicate rows which can lead to incorrect results if left unchecked.The duplicated() method in Pandas helps us to find these duplica
2 min read