janitor.concatenate_columns

janitor.concatenate_columns(df: pandas.core.frame.DataFrame, column_names: List[Hashable], new_column_name, sep: str = '-') → pandas.core.frame.DataFrame[source]

Concatenates the set of columns into a single column.

Used to quickly generate an index based on a group of columns.

This method mutates the original DataFrame.

Functional usage syntax:

df = concatenate_columns(df,
                         column_names=['col1', 'col2'],
                         new_column_name='id',
                         sep='-')

Method chaining syntax:

df = (pd.DataFrame(...).
      concatenate_columns(column_names=['col1', 'col2'],
                          new_column_name='id',
                          sep='-'))
Parameters
  • df – A pandas DataFrame.

  • column_names – A list of columns to concatenate together.

  • new_column_name – The name of the new column.

  • sep – The separator between each column’s data.

Returns

A pandas DataFrame with concatenated columns.

Raises

JanitorError – if at least two columns are not provided within column_names.