Pandas: Bin Continuous Numerical Data with cut() & qcut()

Bin continuous numerical data into discrete categorical intervals using Pandas pd.cut() and pd.qcut(). Transform numerical scores or prices into clean categories, define custom bin boundaries, apply text labels, and compare equal-width vs equal-frequency quantile binning.

Introduction to Data Binning 🔝

Data binning (or bucketization) converts continuous numerical values into discrete categorical bins. Pandas provides two primary functions for this task:
  • pd.cut(): Bins values into equal-width intervals or custom defined ranges.
  • pd.qcut(): Bins values into equal-frequency quantiles (e.g., quartiles, percentiles).

Create numerical sample dataset 🔝

We start with a student exam score dataset to demonstrate numerical binning.
import pandas as pd
df = pd.DataFrame({
    "NAME": ["Ravi", "Raju", "Alex", "John", "Sara", "Priya"],
    "SCORE": [35, 52, 68, 74, 88, 95]
})
print(df)
assert df.shape == (6, 2)

Expected output

    NAME  SCORE
0   Ravi     35
1   Raju     52
2   Alex     68
3   John     74
4   Sara     88
5  Priya     95

Equal-width binning with pd.cut() 🔝

Pass an integer number of bins to pd.cut() to divide the range between the minimum and maximum scores into equal-width segments.
df["WIDTH_BIN"] = pd.cut(df["SCORE"], bins=3)
print(df[["NAME", "SCORE", "WIDTH_BIN"]])
assert df["WIDTH_BIN"].nunique() == 3

Expected output

    NAME  SCORE       WIDTH_BIN
0   Ravi     35   (34.94, 55.0]
1   Raju     52   (34.94, 55.0]
2   Alex     68    (55.0, 75.0]
3   John     74    (55.0, 75.0]
4   Sara     88    (75.0, 95.0]
5  Priya     95    (75.0, 95.0]

Custom bin boundaries and labels 🔝

Supply a list of numeric cut points to bins and custom string category names to labels.
bins = [0, 50, 75, 100]
labels = ["Fail", "Average", "Excellent"]
df["GRADE"] = pd.cut(df["SCORE"], bins=bins, labels=labels)
print(df[["NAME", "SCORE", "GRADE"]])
assert df.loc[0, "GRADE"] == "Fail" and df.loc[5, "GRADE"] == "Excellent"

Expected output

    NAME  SCORE      GRADE
0   Ravi     35       Fail
1   Raju     52    Average
2   Alex     68    Average
3   John     74    Average
4   Sara     88  Excellent
5  Priya     95  Excellent

Equal-frequency binning with pd.qcut() 🔝

Use pd.qcut() to divide data into quantile-based bins containing an approximately equal number of sample observations per bin.
df["TERTILE"] = pd.qcut(df["SCORE"], q=3, labels=["Low", "Medium", "High"])
print(df[["NAME", "SCORE", "TERTILE"]])
assert df["TERTILE"].value_counts().tolist() == [2, 2, 2]

Expected output

    NAME  SCORE TERTILE
0   Ravi     35     Low
1   Raju     52     Low
2   Alex     68  Medium
3   John     74  Medium
4   Sara     88    High
5  Priya     95    High

Key differences: cut() vs qcut() 🔝

Understanding when to select cut() versus qcut():
  • pd.cut(): Best when you have fixed domain cutoffs (e.g., grading scale 0-50-75-100 or age brackets 0-18-65+). Bin intervals are fixed width, but number of items per bin may vary.
  • pd.qcut(): Best when you need balanced sample sizes (e.g., income quartiles or top 25% performers). Bin boundaries adjust dynamically based on quantiles so each bin contains equal counts.

Exercise: bin student exam scores 🔝

Bin a Series of customer ages [18, 25, 38, 45, 62, 70] into 3 equal-frequency quantiles using pd.qcut() with labels ["Young", "Middle", "Senior"]. Verify that each bin contains exactly 2 customers.

Exercise solution 🔝

Solution creating the Series and applying pd.qcut().
ages = pd.Series([18, 25, 38, 45, 62, 70], name="AGE")
answer = pd.qcut(ages, q=3, labels=["Young", "Middle", "Senior"])
print(answer)
assert answer.value_counts().tolist() == [2, 2, 2]

Expected output

0     Young
1     Young
2    Middle
3    Middle
4    Senior
5    Senior
Name: AGE, dtype: category
Categories (3, object): ['Young' < 'Middle' < 'Senior']

Practice in Google Colab 🔝

Interactive Colab Notebook

Practice cut() and qcut() examples interactively directly in your browser:

Open In Colab View on GitHub

Reference: Pandas cut() documentation and qcut() 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