janitor.expand_column

janitor.expand_column(df: pandas.core.frame.DataFrame, column_name: Hashable, sep: str, concat: bool = True) → pandas.core.frame.DataFrame[source]

Expand a categorical column with multiple labels into dummy-coded columns.

Super sugary syntax that wraps pandas.Series.str.get_dummies().

This method does not mutate the original DataFrame.

Functional usage syntax:

df = expand_column(df,
                   column_name='col_name',
                   sep=', ')  # note space in sep

Method chaining syntax:

import pandas as pd
import janitor
df = pd.DataFrame(...).expand_column(column_name='col_name',
                                     sep=', ')
Parameters
  • df – A pandas DataFrame.

  • column_name – Which column to expand.

  • sep – The delimiter. Example delimiters include |, , `, `, etc.

  • concat – Whether to return the expanded column concatenated to the original dataframe (concat=True), or to return it standalone (concat=False).

Returns

A pandas DataFrame with an expanded column.