Here are several approaches to flatten hierarchical index in Pandas DataFrame:

(1) Flatten column MultiIndex with method to_flat_index:

df.columns = df.columns.to_flat_index()

(2) Flatten hierarchical index in DataFrame with .get_level_values(0):

df.columns = df.columns.get_level_values(0) + '_' +  df.columns.get_level_values(1)

(3) Pandas MultiIndex can be flatten with reset_index(drop=True):

df.T.reset_index(drop=True).T

MultiIndex can be flatten on rows and columns.

Next you can find several examples demonstrating how to use the above approaches.

Step 1: Create DataFrame with MultiIndex

Let's say that you have the following DataFrame with hierarchical index on columns:

import pandas as pd

cols = pd.MultiIndex.from_tuples([('company', 'rank'), ('company', 'points')])
df = pd.DataFrame([[1,2], [3,4]], columns=cols)

data:

company
rank points
1 2
3 4

Step 2: Flatten column MultiIndex with method to_flat_index

To flatten hierarchical index on columns or rows we can use the native Pandas method - to_flat_index.

The method is described as:

Convert a MultiIndex to an Index of Tuples containing the level values.

df.columns = df.columns.to_flat_index()

This will change the MultiIndex to a normal index. From:

MultiIndex([('company',   'rank'),
            ('company', 'points')],
           )

to:

Index([('company', 'rank'), ('company', 'points')], dtype='object')

Step 3: Flatten hierarchical index in DataFrame with .get_level_values(0)

An alternative solution which gives control on the levels and the final format is - .get_level_values(0).

Instead of tuples we can get string concatenation of all levels of the MultiIndex.

This method will return the levels accessed by their level number. So to access the highest level we can use 0, then 1 etc.

The method is described as:

Return an Index of values for requested level.

And to convert a MultiIndex levels to a simple index for columns we can combine all levels from the DataFrame:

df.columns = df.columns.get_level_values(0) + '_' +  df.columns.get_level_values(1)

result:

Index(['company_rank', 'company_points'], dtype='object')

Step 4: Pandas flatten MultiIndex by reset_index(drop=True)

Method reset_index can flatten hierarchical index on rows and/or columns.

The usage for columns is a bit more complicated so we will share it as an example.

The method will reset all levels and will reindex the columns

df.T.reset_index(drop=True).T

result:

RangeIndex(start=0, stop=2, step=1)

Step 5: Flatten MultiIndex in Pandas with list comprehension

Finally let's cover the simple usage of Python list comprehension on column MultiIndex.

This method is flexible and you have control of the output.

Similar to Step 2 we are going to iterate through all levels and concatenate the values with symbol - &:

df.columns = [' & '.join(col).rstrip('_') for col in df.columns.values]

result:

Index(['company & rank', 'company & points'], dtype='object')

Resources