reindex_and_groupby_sum#
- caf.toolkit.pandas_utils.df_handling.reindex_and_groupby_sum(df, index_cols, value_cols, throw_error=True, **kwargs)[source]#
Reindexes and groups a pandas DataFrame.
Wrapper around df.reindex() and df.groupby(). Optionally throws an error if index_cols aren’t in df. Will throw an error by default
- Parameters:
df (DataFrame) – The pandas.DataFrame that should be reindexed and grouped.
index_cols (list[str]) – List of column names to reindex to.
value_cols (list[str]) – List of column names that contain values. df.groupby() will be performed on any columns that remain in index_cols once all value_cols have been removed.
throw_error (bool) – Whether to throw an error if not all index_cols are in the df.
- Returns:
A copy of df that has been reindexed and grouped.
- Return type:
new_df
- Raises:
ValueError: – If any of index_cols don’t exist within df and throw_error is True.
See also
caf.toolkit.pandas_utils.df_handling.reindex_cols()