AppDividend
Latest Code Tutorials

Pandas rank: Understanding Pandas DataFrame rank()

0

Pandas DataFrame rank() function returns a rank of every index of the Series, which is passed. The rank is returned based on the position after sorting the Series.

Pandas DataFrame rank()

Pandas DataFrame.rank() method returns the rank of every respective index of a Series passed. The rank is returned based on the position after sorting. The rank() method takes up to six parameters and returns the rank of every index of the Series.

Syntax

DataFrame.rank(axis=0, method=’average’, numeric_only=None, na_option=’keep’, ascending = True, pct = False)

Parameters

It has the following parameters which are defined briefly: 

  1. axis: It takes integer values ( 0 or 1). 0 (index) is for rows, and 1(columns) is for the column.
  2. method: It takes string input. It can have values ‘average’, ‘min,’ ‘max,’ ‘first,’ ‘dense’. By default, it is set to ‘average.’ It means to assign an average of ranks to similar values.
  3. numeric_only: It takes a boolean value and is entirely optional. For DataFrame objects, it ranks only numeric columns if set to True.
  4. na_option: It also takes string input. It can have ‘keep’, ‘top’, ‘bottom’. It is done to set the position of Null values, if any, in the passed Series.
  5. ascending: It takes boolean values that rank in ascending order if True.
  6. pct: It takes a boolean value, which ranks percentage-wise if True.

Return Value

The rank function returns Series with a rank of every index present in the caller Series.

Example program on Pandas DataFrame rank()

Write a program to show the working of rank() method.

import pandas as pd

data_dict = {"Name": ["Rohit", "Karan", "Shivam", "Anubhav"],
             "Age": [21, 23, 31, 23], "City": ["Patna", "Kolkata", "Mumbai", "Kolkata"]}  # Creating a DataFrame with data of few people
data = pd.DataFrame(data_dict)

# Adding a rank column that will be evaluated according to the names.
data["Rank"] = data["Name"].rank()
print(data, "\n")  # Printing the datan

# Sorting the data w.r.t to column == Name
data.sort_values("Name", inplace=True)
print("\n Data after sorting the DataFrame according to the rank")
print(data, "\n")

Output

   Name  Age     City  Rank
0    Rohit   21    Patna   3.0
1    Karan   23  Kolkata   2.0
2   Shivam   31   Mumbai   4.0
3  Anubhav   23  Kolkata   1.0


 Data after sorting the DataFrame according to the rank
      Name  Age     City  Rank
3  Anubhav   23  Kolkata   1.0
1    Karan   23  Kolkata   2.0
0    Rohit   21    Patna   3.0
2   Shivam   31   Mumbai   4.0

Here we can see that we have created a DataFrame from a Dictionary that consists of the data of students. We added a column named rank, and after that, sorted the data according to name and printed in the DataFrame.

Sorting a column with similar values in Pandas

In the following example, the DataFrame is first sorted with respect to the column name.

See the following code.

import pandas as pd

data_dict = {"Name": ["Rohit", "Karan", "Shivam", "Karan"],
             "Age": [21, 23, 31, 23], "City": ["Patna", "Kolkata", "Mumbai", "Kolkata"]}  # Creating a DataFrame with data of few people
data = pd.DataFrame(data_dict)

# Adding a rank column that will be evaluated according to the names.
data["Rank"] = data["Name"].rank()

print(data, "\n")  # Printing the data
# Sorting the data w.r.t to column == Name
data.sort_values("Name", inplace=True)
print("\n Data after sorting the DataFrame according to the rank")
print(data, "\n")

Output

   Name  Age     City  Rank
0   Rohit   21    Patna   3.0
1   Karan   23  Kolkata   1.5
2  Shivam   31   Mumbai   4.0
3   Karan   23  Kolkata   1.5


 Data after sorting the DataFrame according to the rank
     Name  Age     City  Rank
1   Karan   23  Kolkata   1.5
3   Karan   23  Kolkata   1.5
0   Rohit   21    Patna   3.0
2  Shivam   31   Mumbai   4.0

Here we can see that we have created a DataFrame from a Dictionary that consists of the data of students having the same names (Karan in this DataFrame). We added a column named rank, and after that, sorted the data according to name and printed in the DataFrame.

That is it for the Pandas DataFrame rank() method.

See also

Pandas DataFrame apply()

Pandas DataFrame merge()

Pandas DataFrame fillna()

Pandas DataFrame from_dict()

Pandas DataFrame drop()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.