> ## 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 Drop a Level from a MultiIndex in Pandas DataFrame
- URL: https://datascientyst.com/pandas-drop-multiindex-level/
- Published: 2021-07-11T08:43:20.000Z
- Updated: 2021-11-12T12:44:34.000Z
- Author: John D K
- Tags: MultiIndex

Here are several approaches to **drop levels of MultiIndex in a Pandas DataFrame**:

- `droplevel` \- completely drop MultiIndex level
- `reset_index` \- remove levels of MultiIndex while storing data into columns/rows

If you want to find more about: [What is a DataFrame MultiIndex in Pandas](https://datascientyst.com/dataframe-multiindex-in-pandas)

## Step 1: Pandas drop MultiIndex by method - droplevel

### Pandas drop MultiIndex on index/rows

Method `droplevel()` will remove one, several or all levels from a MultiIndex. Let's check the default execution by next example:

```python
import pandas as pd

cols = pd.MultiIndex.from_tuples([(0, 1), (0, 1)])
df = pd.DataFrame([[1,2], [3,4]], index=cols)
df

```

|   |   | 0 | 1 |
| - | - | - | - |
| 0 | 1 | 1 | 2 |
| 1 | 3 | 4 |   |

We can drop level 0 of the MultiIndex by:

```python
df.droplevel(level=0)

```

which will result in:

|   | 0 | 1 |
| - | - | - |
| 1 | 1 | 2 |
| 1 | 3 | 4 |

or level 1:

```python
df.droplevel(level=1)

```

### Pandas drop MultiIndex on columns

If the hierarchical indexing is on the columns then we can drop levels by parameter `axis`:

```python
df.droplevel(level=0, axis=1)

```

### Get column names of the dropped columns

If you like to get the names of the columns which will be dropped you can use next syntax:

- for columns:

```python
df.columns.droplevel(level=0)

```

- for rows:

```python
df.index.droplevel(level=0)

```

which will result of single index:

> Int64Index(\[1, 1\], dtype='int64')

### Pandas drop MultiIndex with `.columns.droplevel(level=0)`

So we can drop level of MultiIndex with a simple reset of the column names like:

```python
df.columns = df.columns.droplevel(level=0)

```

![drop-level-from-multiindex-pandas](https://datascientyst.com/content/images/2021/07/drop-level-from-multiindex-pandas.png)

## Step 2: Pandas drop MultiIndex to column values by reset\_index

### Drop all levels of MultiIndex to columns

Use reset\_index if you like to drop the MultiIndex while keeping the information from it. Let's do a quick demo:

```python
import pandas as pd

cols = pd.MultiIndex.from_tuples([(0, 1), (0, 1)])
df = pd.DataFrame([[1,2], [3,4]], index=cols)

```

|   |   | 0 | 1 |
| - | - | - | - |
| 0 | 1 | 1 | 2 |
| 1 | 3 | 4 |   |

After reset of the MultiIndex we will get:

```python
df.reset_index()

```

|   | level\_0 | level\_1 | 0 | 1 |
| - | -------- | -------- | - | - |
| 0 | 0        | 1        | 1 | 2 |
| 1 | 0        | 1        | 3 | 4 |

### Reset single level of MultiIndex

```python
df.reset_index(level=1)

```

## Typical Errors on Pandas Drop MultiIndex

When the droplevel is invoked on wrong axis: columns or rows like:

```python
cols = pd.MultiIndex.from_tuples([(0, 1), (0, 1)])
df = pd.DataFrame([[1,2], [3,4]], index=cols)

df.columns.droplevel()

```

error is raised:

> ValueError: Cannot remove 1 levels from an index with 1 levels: at least one level must be left.

Another example is when the levels are less the one which should be dropped:

```python
cols = pd.MultiIndex.from_tuples([(0, 1), (0, 1)])
df = pd.DataFrame([[1,2], [3,4]], index=cols)

df.columns.droplevel(level=3)

```

> IndexError: Too many levels: Index has only 1 level, not 4

## Resources

- [Pandas drop MultiIndex](https://github.com/softhints/datascientyst/blob/master/multiindex/2.pandas-drop-multiindex.ipynb?ref=datascientyst.com)
- [pandas.MultiIndex.droplevel](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.MultiIndex.droplevel.html?ref=datascientyst.com)
- [1pandas.DataFrame.reset\_index](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.reset%5Findex.html?ref=datascientyst.com)