reindex_cols#

caf.toolkit.pandas_utils.reindex_cols(df, columns, throw_error=True, dataframe_name='the given dataframe', **kwargs)[source]#

Reindexes a pandas DataFrame. Will throw error if columns aren’t in df.

Parameters:
  • df (DataFrame) – The pandas.DataFrame that should be re-indexed

  • columns (list[str]) – The columns to re-index df to.

  • throw_error (bool) – Whether to throw an error or not if the given columns don’t exist in df. If False, then operates exactly like calling df.reindex() directly.

  • dataframe_name (str) – The name to give to the dataframe in the error message being thrown.

  • kwargs – Any extra arguments to pass into df.reindex()

Returns:

df, re-indexed to only have columns as column names.

Return type:

re-indexed_df

Raises:

ValueError: – If any of columns don’t exist within df and throw_error is True.