Column-wise ordering
Compare observations within each feature by ordering values from smallest to largest or vice versa.
Pandas · Module
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.
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.
DataFrame.rank(axis=0, method='average', numeric_only=False, na_option='keep', ascending=True, pct=False)| Name | Type | Description |
|---|---|---|
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_only | bool | For DataFrames, rank only numeric columns when True. Default: False. |
na_option | {'keep', 'top', 'bottom'} | Missing-value policy: 'keep', 'top', or 'bottom'. Default: 'keep'. |
ascending | bool | Rank in ascending order when True; default: True. |
pct | bool | Return rankings in percentile form when True. Default: False. |
same type as caller: a Series or DataFrame containing ranks.
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.
Compare observations within each feature by ordering values from smallest to largest or vice versa.
Create per-row leaderboards across several measurements with axis=1.
Convert tied values into contiguous group numbers using method='dense'.
Express a value's relative rank as a percentile with pct=True.
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'.
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.
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.rankDataFrameGroupBy.rankpandas.core.window.expanding.Expanding.rank