> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Flatten a MultiIndex in Pandas
- URL: https://datascientyst.com/flatten-multiindex-in-pandas/
- Published: 2021-09-06T20:37:08.000Z
- Updated: 2021-12-02T10:49:32.000Z
- Author: John D K
- Tags: MultiIndex

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

(1) Flatten column MultiIndex with method `to_flat_index`:

```python
df.columns = df.columns.to_flat_index()

```

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

```python
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)`:

```python
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:

```python
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.

```python
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:

```python
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

```python
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 - `&`:

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

```

result:

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

```

## Resources

- [Notebook](https://github.com/softhints/datascientyst/blob/master/multiindex/4.flatten-multiindex-in-pandas.ipynb?ref=datascientyst.com)
- [pandas.MultiIndex.to\_flat\_index](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.MultiIndex.to%5Fflat%5Findex.html?ref=datascientyst.com)
- [pandas.Index.get\_level\_values](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Index.get%5Flevel%5Fvalues.html?ref=datascientyst.com)