Pandas DataFrame rank() Method


Preparation

Before any data manipulation can occur, two (2) new libraries will require installation.

  • The Pandas library enables access to/from a DataFrame.
  • The NumPy library supports multi-dimensional arrays and matrices in addition to a collection of mathematical functions.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install numpy

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.


Feel free to view the PyCharm installation guide for the required libraries.


Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd
import numpy as np 

DataFrame rank()

The rank() method returns a DataFrame/Series with the values ranked in order. The return value is the same as the caller.

The syntax for this method is as follows:

DataFrame.rank(axis=0, method='average', numeric_only=None, na_option='keep', ascending=True, pct=False)
ParameterDescription
axisIf zero (0) or index is selected, apply to each column. Default 0.
If one (1) apply to each row.
methodDetermines how to rank identical values, such as:
– The average rank of the group.
– The lowest (min) rank value of the group.
– The highest (max) rank value of the group.
– Each assigns in the same order they appear in the array.
– Density increases by one (1) between the groups.
numeric_onlyOnly include columns that contain integers, floats, or boolean values.
na_optionDetermines how NaN values rank, such as:
– Keep assigns a NaN to the rank values.
– Top: The lowest rank to any NaN values found.
– Bottom: The highest to any NaN values found.
ascendingDetermines if the elements/values rank in ascending or descending order.
pctIf set to True, the results will return in percentile form. By default, this value is False.

For this example, a CSV file is read in and is ranked on Population and sorted. Click here to download and move this file to the current working directory.

df = pd.read_csv("countries.csv")
df["Rank"] = df["Population"].rank()
df.sort_values("Population", inplace=True)
print(df)
  • Line [1] reads in the countries.csv file and saves it to df.
  • Line [2] appends a column to the end of the DataFrame (df). 
  • Line [3] sorts the CSV file in ascending order.
  • Line [4] outputs the result to the terminal.

Output

 CountryCapitalPopulationAreaRank
4PolandWarsaw383830003126851.0
2SpainMadrid474312564985112.0
3ItalyRome603171163013383.0
1FranceParis670810005516954.0
0GermanyBerlin837839423570215.0
5RussiaMoscow146748590170982466.0
6USAWashington32823952398335207.0
8IndiaDheli135264228032872638.0
7ChinaBeijing140005000095969619.0

More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.