Home >Backend Development >Python Tutorial >How to Flatten a Hierarchical Column Index in Pandas DataFrames?
Flattening Hierarchical Column Index in DataFrames
When working with data frames, a hierarchical index in the columns (axis 1) can arise from a groupby operation. The data frame will have sub-headers with multiple levels. However, flattening the index to create a single-level header is often desirable.
To flatten a hierarchical column index, there are two options:
1. Setting Columns to Top Level:
This option moves the current top-level headers to the column names directly. Using the get_level_values method along with 0, which represents the first level, we can set the columns as follows:
df.columns = df.columns.get_level_values(0)
2. Combining MultiIndex into One Index:
This option joins the multi-index column headers into a single index. Assuming the columns contain strings, the following code can be used:
df.columns = [' '.join(col).strip() for col in df.columns.values]
The strip() function ensures that any whitespace is removed from the combined header. After executing this code, the hierarchical column index will be flattened.
The above is the detailed content of How to Flatten a Hierarchical Column Index in Pandas DataFrames?. For more information, please follow other related articles on the PHP Chinese website!