Signature
pd.cut(x, bins, right=True, labels=None)What it does
pd.cut turns a numeric column into ordered bins. Equal-width bins are easy to explain, while quantile bins keep group sizes balanced; pass explicit edges when the boundaries have real meaning.
Parameters
xarray_like- Values to bin.
binsint, sequence, or IntervalIndex- Number of equal-width bins or explicit edges.
labelsarray_like, optionaloptional- Names for the resulting categories.
Returns
Categorical
Example
pythonpd.cut example
import pandas as pd
age = pd.Series([18, 25, 41, 67])
print(pd.cut(age, bins=[0, 30, 60, 100], labels=["young", "mid", "senior"]))Output
0 young 1 young 2 mid 3 senior dtype: category
Watch out for
- The right edge is included by default; check the boundary rows.
- Bins derived from the full dataset should be reused on test data, not recomputed.