janitor.transform_columns

janitor.transform_columns(df: pandas.core.frame.DataFrame, column_names: Union[List[str], Tuple[str]], function: Callable, suffix: Optional[str] = None, elementwise: bool = True, new_column_names: Optional[Dict[str, str]] = None) → pandas.core.frame.DataFrame[source]

Transform multiple columns through the same transformation.

This method mutates the original DataFrame.

Super syntactic sugar!

Basically wraps transform_column and calls it repeatedly over all column names provided.

User can optionally supply either a suffix to create a new set of columns with the specified suffix, or provide a dictionary mapping each original column name to its corresponding new column name. Note that all column names must be strings.

A few examples below. Firstly, to just log10 transform a list of columns without creating new columns to hold the transformed values:

df = (
    pd.DataFrame(...)
    .transform_columns(['col1', 'col2', 'col3'], np.log10)
)

Secondly, to add a ‘_log’ suffix when creating a new column, which we think is going to be the most common use case:

df = (
    pd.DataFrame(...)
    .transform_columns(
        ['col1', 'col2', 'col3'],
        np.log10,
        suffix="_log"
    )
)

Finally, to provide new names explicitly:

df = (
    pd.DataFrame(...)
    .transform_column(
        ['col1', 'col2', 'col3'],
        np.log10,
        new_column_names={
            'col1': 'transform1',
            'col2': 'transform2',
            'col3': 'transform3',
            }
        )
)
Parameters
  • df – A pandas DataFrame.

  • column_names – An iterable of columns to transform.

  • function – A function to apply on each column.

  • suffix – (optional) Suffix to use when creating new columns to hold the transformed values.

  • elementwise – Passed on to transform_column; whether or not to apply the transformation function elementwise (True) or columnwise (False).

  • new_column_names – (optional) An explicit mapping of old column names to new column names.

Returns

A pandas DataFrame with transformed columns.

Raises

ValueError – if both suffix and new_column_names are specified