Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pandas · Module

df.rank

DataFrame.rank assigns numerical ranks along columns by default or across rows when requested. It supports configurable tie, missing-value, direction, numeric-column, and percentile behavior.

How it works

DataFrame.rank computes numerical ranks independently along each column by default, or across each row when axis=1. It lets you choose tie handling, NaN placement, sort direction, numeric-column filtering, and percentile output while preserving the caller's Series or DataFrame shape.

Syntax

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

Parameters

NameTypeDescription
axis{0 or 'index', 1 or 'columns'}Axis used for ranking: 0 or 'index' ranks down columns; 1 or 'columns' ranks across rows. Default: 0.
method{'average', 'min', 'max', 'first', 'dense'}Tie-ranking strategy: 'average', 'min', 'max', 'first', or 'dense'. Default: 'average'.
numeric_onlyboolFor DataFrames, rank only numeric columns when True. Default: False.
na_option{'keep', 'top', 'bottom'}Missing-value policy: 'keep', 'top', or 'bottom'. Default: 'keep'.
ascendingboolRank in ascending order when True; default: True.
pctboolReturn rankings in percentile form when True. Default: False.

Returns

same type as caller: a Series or DataFrame containing ranks.

Key points

  • The method produces numerical ranks along either DataFrame axis.
  • axis accepts 0 or 'index' for column-wise ranking, and 1 or 'columns' for row-wise ranking.
  • method controls how tied values are ranked: average, min, max, first, or dense.
  • ascending=True gives smaller values lower ranks; set it to False to rank larger values first.
  • na_option determines whether missing values remain missing or receive the lowest or highest rank.
  • The result keeps the same general type as the caller: a DataFrame for DataFrame.rank.

Common mistakes

  • The default tie strategy is average, so equal values can receive fractional ranks; choose min, max, first, or dense when a different policy is needed.
  • numeric_only=True excludes nonnumeric columns; the default is False.
  • pct=True returns percentile-style ranks rather than ordinary ordinal ranks.

Example: Rank rows with ties

import pandas as pd

scores = pd.DataFrame({"alice": [80, 95], "bob": [90, 95]})
print(scores.rank(axis=1, method="dense", ascending=False))

Ranks each row from highest score to lowest, assigning equal scores the same dense rank.

When to use it

Column-wise ordering

Compare observations within each feature by ordering values from smallest to largest or vice versa.

Row-wise comparisons

Create per-row leaderboards across several measurements with axis=1.

Dense tie groups

Convert tied values into contiguous group numbers using method='dense'.

Percentile rankings

Express a value's relative rank as a percentile with pct=True.

Interview questions

QWhat does DataFrame.rank do, and how are ties handled by default?

DataFrame.rank assigns numerical ranks from 1 through n along the chosen axis. With the default method='average', tied values receive the mean of the ranks they occupy.

Follow-up: How would you rank values within rows and make ties share consecutive group numbers? Use axis=1 and method='dense'.

QHow does axis change DataFrame.rank's behavior?

axis=0, also called 'index', ranks down each column. axis=1, also called 'columns', ranks across each row.

Follow-up: How can missing values be placed at the beginning? Set na_option='top'; use 'bottom' to place them at the highest ranks.

QWhen would you choose method='first' versus method='dense'?

The method choices are 'average', 'min', 'max', 'first', and 'dense'. 'first' breaks ties by their order of appearance, while 'dense' increases the rank by one between distinct value groups.

Follow-up: What does pct=True change? It returns rankings in percentile form instead of the usual integer-like rank scale.

  • Series.rank
  • DataFrameGroupBy.rank
  • pandas.core.window.expanding.Expanding.rank

Source: pandas.DataFrame.rank. Checked 2026-10-15.