Ciaren

Bin column — binColumn

Bucket a numeric column into labeled bins (a new column).

Use cases

  • Turn ages or amounts into bands (0–18, 19–35, …).
  • Create quantile buckets (quartiles, deciles) for segmentation.

What it does

Reads a numeric column and writes a new text column whose values are the bin labels. equalwidth makes bins of equal numeric span; quantile makes bins with roughly equal row counts.

Bin column (column=total_spent, bins=3, method=quantile, labels=[Bronze,Silver,Gold])
Before
nametotal_spent
Grace500
Margaret250
Ada100
Linus15
4 rows · 2 cols
After
nametotal_spenttiernew
Grace500Gold
Margaret250Silver
Ada100Bronze
Linus15Bronze
4 rows · 3 cols

Configuration

Config keyTypeRequiredDescription
columnstringYesNumeric column to bin
new_columnstringYesName of the new bin column
binsintNoNumber of bins (≥ 2, default 4)
methodstringNoequalwidth (default) or quantile
labelsstring[]NoCustom labels (one per bin)

Generated Python code

This example bins amount into four equal-width bands named amount_band, using the default method and labels.

pandas

python
df_2 = df_1.assign(amount_band=pd.cut(df_1['amount'], bins=4).astype('string'))

Polars

python
_lo, _hi = df_1['amount'].min(), df_1['amount'].max()
_breaks = [_lo + (_hi - _lo) / 4 * i for i in range(1, 4)]
df_2 = df_1.with_columns(pl.col('amount').cut(_breaks).cast(pl.Utf8).alias('amount_band'))

Tips & common mistakes

  • equalwidth vs quantile: equal-width splits the value range into equal spans (uneven counts); quantile splits into equal-sized groups (uneven spans).
  • Custom labels must match the bin count (one label per bin).
  • The result is a text/category column — feed it into Group by + aggregate to count per band.

See also