Binning Numbers into Categories
Intuition
Ages 0–120 are easier to report as "child / teen / adult / senior". Binning converts a continuous variable into ordered categories.
Precise explanation
pd.cut(ages, bins=[0, 13, 20, 65, np.inf], labels=["child", "teen", "adult", "senior"], right=False)
binsare edges;right=Falsemakes intervals left-closed[0, 13),[13, 20), … so 13 is a teen. The defaultright=Truegives(0, 13].- Values outside all bins become NaN.
- The result is an ordered Categorical;
value_counts(sort=False)lists categories in bin order and includes empty categories with count 0. pd.qcutmakes equal-frequency bins from quantiles instead of fixed edges.
Worked example
ages [5, 13, 19, 20, 70, -1] → [child, teen, teen, adult, senior, NaN].
Common mistakes
- Off-by-one at bin edges from the wrong
rightsetting. - Forgetting
np.infas the last edge, so large values become NaN. - Sorting counts by frequency when the category order is meaningful.
Why this matters
Binned features help simple models capture non-linear effects and are used in calibration, fairness reporting and histograms (ST-04).
References
- 10 minutes to pandas (pandas 3.1.0 docs) — pandas via NumFOCUS (
S008)