Q&A library Python

How do I calculate quantiles and percentile ranks in pandas?

Python Advanced percentilequantilerank

Short answer

Use quantile to find the value at a given percentile, and rank with pct=True to find each value's percentile position. They answer opposite questions, and the interpolation method affects the quantile result.

The short version

quantile(0.25) returns the value below which a quarter of the data falls. rank(pct=True) does the reverse, giving each value its position as a fraction from 0 to 1. Use quantile to ask "what value marks the 25th percentile" and percentile rank to ask "what percentile is this value at". The two are inverses of each other.

Finding the value at a percentile

import pandas as pd

s = pd.Series([1, 2, 3, 4])

print(s.quantile(0.5))    # 2.5, the median
print(s.quantile(0.25))   # 1.75, the lower quartile

quantile accepts a single probability or a list, so s.quantile([0.25, 0.5, 0.75]) returns all three quartiles at once.

Finding each value's percentile

s.rank(pct=True).tolist()   # [0.25, 0.5, 0.75, 1.0]

Here each value is expressed as its rank divided by the count, so the largest value sits at 1.0 (the 100th percentile). This is how you turn raw values into percentile positions, useful for comparing across differently scaled columns.

Why the quartile is 1.75, not 2

The 25th percentile of four values falls between the first and second, so pandas interpolates by default, giving 1.75. Different tools use different rules, so a quartile that does not match another package is usually an interpolation difference, not an error. Control it with the interpolation argument.

s.quantile(0.25, interpolation="lower")   # 1, no interpolation
s.quantile(0.25, interpolation="nearest") # nearest actual value

Choose the method your context expects. Finance and statistics teams often have a house convention, so match it rather than accepting the default blindly.

Quantiles per group

For quantiles within groups, combine with groupby.

df.groupby("city")["value"].quantile(0.9)   # each city's 90th percentile

This is the natural way to find, say, a high-percentile threshold separately for each group.

Relation to describe

describe already reports the 25th, 50th and 75th percentiles as a quick summary. Use describe for the standard overview, and quantile when you need a specific percentile it does not show (like the 90th or 99th) or a custom set.

A note on outliers

Percentiles are far more robust to outliers than the mean, because a single extreme value barely moves them. That is why percentile-based measures (the median, the interquartile range) are preferred for skewed data, where a mean would be dragged towards the extreme and mislead.

Need this solved on your own data?

The library is free and always will be. When the problem is bigger than an answer, we take on the work.

Start a project