Pandas: Calculate Quantiles & Percentiles with quantile()

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.

Introduction to quantile() 🔝

The 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.

Create numerical sample Series 🔝

We start with a sequential numerical Series to calculate percentiles.
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

Single quantile calculation 🔝

Pass a single float value (such as 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

Computing multiple quantiles simultaneously 🔝

Pass a list of quantile fractions to 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

Exercise: compute 90th percentile 🔝

Given a Series 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.

Exercise solution 🔝

Solution creating the Series and calculating 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 in Google Colab 🔝

Interactive Colab Notebook

Practice quantile() examples interactively directly in your browser:

Open In Colab View on GitHub

Reference: Pandas quantile() documentation.




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer