pydit.wrangling.collapse_dataframe_levels.collapse_levels¶
- pydit.wrangling.collapse_dataframe_levels.collapse_levels(obj: DataFrame, sep: str = '_') DataFrame[source]¶
Flatten multi-level column dataframe to a single level.
This method does not mutate the original DataFrame.
Given a DataFrame containing multi-level columns, flatten to single-level by string-joining the column labels in each level.
After a groupby / aggregate operation where .agg() is passed a list of multiple aggregation functions, a multi-level DataFrame is returned with the name of the function applied in the second level.
It is sometimes convenient for later indexing to flatten out this multi-level configuration back into a single level. This function does this through a simple string-joining of all the names across different levels in a single column.
- Parameters:
obj (pandas.DataFrame) – The DataFrame to flatten.
sep (str, optional, default "_") – The separator to use when joining the column names.
- Returns:
A new pandas DataFrame with single-level column index
- Return type:
pandas.DataFrame