Compute values at specified quantiles or percentiles across Series or DataFrames using the Pandas quantile() method. Evaluate median ($q=0.5$), interquartile ranges, configure interpolation parameters, and compute multiple quantiles simultaneously.
quantile() method returns values at the given quantile $q$ (where $0.0 \le q \le 1.0$). Passing $q=0.5$ calculates the median (50th percentile), while $q=0.25$ and $q=0.75$ evaluate the 25th and 75th percentiles respectively.import pandas as pd
s = pd.Series([10, 20, 30, 40, 50], name="SCORES")
print(s)
assert s.shape == (5,)
Expected output
0 10
1 20
2 30
3 40
4 50
Name: SCORES, dtype: int64
0.5 for 50th percentile) to quantile().q50 = s.quantile(0.5)
print(f"Median (q=0.5): {q50}")
assert q50 == 30.0
Expected output
Median (q=0.5): 30.0
quantile() to compute multiple percentiles in a single operation.q_multi = s.quantile([0.25, 0.75])
print(q_multi)
assert q_multi.loc[0.75] == 40.0
Expected output
0.25 20.0
0.75 40.0
Name: SCORES, dtype: float64
vals = pd.Series([100, 200, 300, 400, 500], name="VALS"), compute its 90th percentile ($q=0.90$) using quantile() and verify that it equals 460.0.quantile().vals = pd.Series([100, 200, 300, 400, 500], name="VALS")
answer = vals.quantile(0.9)
print(answer)
assert answer == 460.0
Expected output
460.0
Practice quantile() examples interactively directly in your browser:
Reference: Pandas quantile() documentation.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.