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

Polars · Module

.rank

Assign ranks to expression values while handling ties through configurable methods. It can rank ascending or descending and can be scoped to groups with over.

How it works

Calling rank on an expression produces an expression containing ranks for the non-null values. Choose a tie strategy with method, optionally reverse the ordering with descending, and provide seed when using random tie assignment; combine it with over to rank within groups.

Syntax

Expr.rank(method: RankMethod = 'average', *, descending: bool = False, seed: int | None = None) → Expr

Parameters

NameTypeDescription
methodRankMethod = 'average'Tie-ranking strategy: average, min, max, dense, ordinal, or random.
descendingbool = FalseRank in descending order when true.
seedint | None = NoneSeed used when method is random.

Returns

Expr

Key points

  • Expr.rank assigns ranks while applying a chosen policy for equal values.
  • The default tie policy is average; other supported choices include min, max, dense, ordinal, and random.
  • Dense ranking keeps the next distinct rank directly after a tied block.
  • Ordinal ranking makes every value distinct according to its series order.
  • Random ranking breaks ties independently of their original order.

Common mistakes

  • Null values are not ranked; their null positions remain null rather than being assigned a rank.
  • The seed parameter matters only for random tie handling; it does not affect the other methods.

Example: Rank values within groups

import polars as pl

df = pl.DataFrame({"group": ["a", "a", "b"], "value": [10, 10, 5]})
result = df.with_columns(
    pl.col("value").rank(method="dense").over("group").alias("rank")
)
print(result)

Ranks values within each group using dense ranking, so tied values share a rank and the next rank is consecutive.

When to use it

Leaderboards

Add a leaderboard position to a DataFrame column, choosing how equal scores share ranks.

Grouped ranking

Compute per-category positions by combining rank with over.

Tie ordering

Create deterministic distinct ordering for ties with ordinal ranking.

Interview questions

QWhat does Expr.rank do, and what are its main options?

rank assigns ordered rank values while applying the selected method to ties. The default method is average, and descending reverses the ranking direction.

Follow-up: How would you rank independently within categories? Use the rank expression with over, such as pl.col("score").rank().over("category").

QHow do the tie methods differ?

The methods are average, min, max, dense, ordinal, and random. Ordinal gives every value a distinct rank based on occurrence order, whereas random breaks ties without depending on that order.

Follow-up: Which method gives consecutive ranks after ties? Dense ranking: tied values receive the minimum-style rank, and the next distinct value receives the immediately following rank.

QWhat does rank return, and how are nulls handled?

The method returns an Expr, so it can be used inside select or with_columns and combined with windowing through over. Only non-null values receive ranks; nulls are preserved.

Follow-up: What should you verify when ranking grouped data with nulls? Confirm that the ranking is scoped with over and that null entries remain null within each group.

  • polars.Expr.rolling_rank
  • polars.Expr.over
  • polars.Expr.sort

Source: polars.Expr.rank — Polars documentation - Polars user guide. Checked 2026-10-15.