Python Central HubSign in

Data Analytics with Python / Phase 4: Data Preprocessing and Cleaning

Binning and Discretization

Convert continuous numeric values into categories using cut and qcut for analysis and modeling.

Why bin values?

Binning (discretization) converts numeric values into categories:

Python
- Age → {0–18, 19–35, 36–60, 60+}
- Spend → {low, medium, high}

Use cases:

  • Easier reporting and segmentation
  • Non-linear relationship capture (sometimes)
  • Feature engineering

pd.cut (fixed bins)

Python
import pandas as pd
 
age = pd.Series([12, 19, 25, 38, 52, 67])
 
bins = [0, 18, 35, 60, 200]
labels = ["0-18", "19-35", "36-60", "60+"]
 
age_group = pd.cut(age, bins=bins, labels=labels, right=True, include_lowest=True)
print(age_group)

pd.qcut (quantile bins)

Quantile binning tries to put roughly equal number of rows in each bin.

Python
import pandas as pd
 
values = pd.Series([10, 15, 20, 25, 30, 40, 60, 100])
 
bucket = pd.qcut(values, q=3, labels=["low", "mid", "high"])
print(bucket)

Tips

  • Choose bins based on domain meaning.
  • Quantile bins are useful when distributions are skewed.
  • Always check counts per bin after binning.

Visualize it

diagram Binning continuous values mermaid
Continuous numeric values are grouped into a small number of discrete bins, either fixed-width or by quantile.
sketch Scattered ages sorted into bins p5.js
Each age lands in exactly one bin based on the bin edges; dot color matches the band it falls into.

🧪 Try It Yourself

Exercise 1 – Bin with fixed edges

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Bin into equal-sized quantile groups

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Count rows per bin

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Check your understanding

quizCheck yourself
  1. What is the main difference between pd.cut and pd.qcut?

    Show answer

    A — cut uses bin edges you choose, qcut uses quantiles so bins hold roughly equal numbers of rows — pd.cut takes explicit bins like [0, 18, 35, 60, 200]. pd.qcut puts roughly the same number of rows in each bin, which helps on skewed data.

  2. With bins = [0, 18, 35, 60, 200] and right=True, which label does an age of 38 get?

    Show answer

    B — 36-60 — The bins are right-inclusive, so 38 falls in (35, 60], labeled 36-60.

  3. What does qcut(values, q=3, labels=["low", "mid", "high"]) create?

    Show answer

    A — Three bins with roughly equal row counts — q=3 splits at the quantiles so each bucket holds about a third of the rows.

  4. Which check does the lesson recommend after binning?

    Show answer

    A — Look at the counts per bin — Always check counts per bin. Choose bins based on domain meaning, and use quantile bins when distributions are skewed.

Next

Bins turn numbers into categories — pair this with Train-Test Split Concepts before you build and evaluate a model on them.

Was this page helpful?

Let us know how we did