janitor.bin_numeric

janitor.bin_numeric(df: pandas.core.frame.DataFrame, from_column_name: Hashable, to_column_name: Hashable, num_bins: int = 5, labels: Optional[str] = None) → pandas.core.frame.DataFrame[source]

Generate a new column that labels bins for a specified numeric column.

This method mutates the original DataFrame.

Makes use of pandas cut() function to bin data of one column, generating a new column with the results.

import pandas as pd
import janitor
df = (
    pd.DataFrame(...)
    .bin_numeric(
        from_column_name='col1',
        to_column_name='col1_binned',
        num_bins=3,
        labels=['1-2', '3-4', '5-6']
        )
)
Parameters
  • df – A pandas DataFrame.

  • from_column_name – The column whose data you want binned.

  • to_column_name – The new column to be created with the binned data.

  • num_bins – The number of bins to be utilized.

  • labels – Optionally rename numeric bin ranges with labels. Number of label names must match number of bins specified.

Returns

A pandas DataFrame.

Raises

ValueError – if number of labels do not match number of bins.