Why bin values?
Binning (discretization) converts numeric values into categories:
- 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)
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.
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
flowchart LR
A["Continuous values"] --> B{"cut or qcut?"}
B -->|"fixed edges"| C["pd.cut(values, bins)"]
B -->|"equal-sized groups"| D["pd.qcut(values, q)"]
C --> E["Category per row"]
D --> E
🧪 Try It Yourself
Exercise 1 – Bin with fixed edges
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Bin into equal-sized quantile groups
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Count rows per bin
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Check your understanding
What is the main difference between pd.cut and pd.qcut?
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.
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.
With bins = [0, 18, 35, 60, 200] and right=True, which label does an age of 38 get?
The bins are right-inclusive, so 38 falls in (35, 60], labeled 36-60.
Show answer
B — 36-60 — The bins are right-inclusive, so 38 falls in (35, 60], labeled 36-60.
What does qcut(values, q=3, labels=["low", "mid", "high"]) create?
q=3 splits at the quantiles so each bucket holds about a third of the rows.
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.
Which check does the lesson recommend after binning?
Always check counts per bin. Choose bins based on domain meaning, and use quantile bins when distributions are skewed.
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.