How to add a Level to Pandas MultiIndex in Python

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
5 min

banner

# Table of Contents

  1. How to add a Level to Pandas MultiIndex in Python
  2. Adding a Level to Columns of a MultiIndex in Pandas
  3. Adding a Level to Pandas MultiIndex by converting to DataFrame
  4. Adding a column level to a Pandas DataFrame

# How to add a Level to Pandas MultiIndex in Python

You can use the pandas.concat() method to add a level to a Pandas MultiIndex.

The concat method takes a names argument that is used to set the names for the levels in the resulting hierarchical index.

main.py
import pandas as pd df = pd.DataFrame({ 'A': [1, 1, 1, 2, 3], 'B': [1, 2, 3, 4, 5] }) df2 = pd.concat([df], keys=['X'], names=['First Level']) # A B # First Level # X 0 1 1 # 1 1 2 # 2 1 3 # 3 2 4 # 4 3 5 print(df2) print('-' * 50) # 👇️ ['First Level', None] print(df2.index.names)

add level to pandas multiindex dataframe

The code for this article is available on GitHub

The pandas.concat() method is used to concatenate Pandas objects along a particular axis.

The keys argument is a sequence whose elements are used to construct the hierarchical index as the outermost level.

The names argument is a list of names for the levels in the resulting hierarchical index.

You can also pass a dictionary as the first argument to concat() to achieve the same result.

main.py
import pandas as pd df = pd.DataFrame({ 'A': [1, 1, 1, 2, 3], 'B': [1, 2, 3, 4, 5] }) df2 = pd.concat({'X': df}, names=['First Level']) # A B # First Level # X 0 1 1 # 1 1 2 # 2 1 3 # 3 2 4 # 4 3 5 print(df2) print('-' * 50) # 👇️ ['First Level', None] print(df2.index.names)
The code for this article is available on GitHub

If you pass a mapping to the pandas.concat() method, the sorted keys are used as the keys argument.

# Adding a Level to Columns of a MultiIndex in Pandas

If you need to add a level to the columns of a MultiIndex, set the axis parameter to 1.

main.py
import pandas as pd df = pd.DataFrame({ 'A': [1, 1, 1, 2, 3], 'B': [1, 2, 3, 4, 5] }) df2 = pd.concat([df], keys=['X'], names=['First Level'], axis=1) # First Level X # A B # 0 1 1 # 1 1 2 # 2 1 3 # 3 2 4 # 4 3 5 print(df2) print('-' * 50) # MultiIndex([('X', 'A'), # ('X', 'B')], # names=['First Level', None]) print(df2.columns)

add level to columns of pandas multiindex

The code for this article is available on GitHub

The axis parameter is used to set the axis to concatenate along.

By default, the axis is set to 0, which means concatenate along the index.

By setting axis to 1, we concatenate along the columns.

# Adding a Level to Pandas MultiIndex by converting to DataFrame

You can also add a level to a MultiIndex by converting the index to a DataFrame.

main.py
import pandas as pd df = pd.DataFrame({ 'A': [1, 1, 1, 2, 3], 'B': [1, 2, 3, 4, 5] }) old_index = df.index.to_frame() values = [11, 12, 13, 14, 15] old_index.insert(0, 'FirstLevel', values) df.index = pd.MultiIndex.from_frame(old_index) # A B # FirstLevel 0 # 11 0 1 1 # 12 1 1 2 # 13 2 1 3 # 14 3 2 4 # 15 4 3 5 print(df)

add level to multiindex by converting to dataframe

The code for this article is available on GitHub
  1. We used the index.to_frame() method to convert the index to a DataFrame.
  2. The next step is to insert the new level at a specific index using DataFrame.insert.

The first argument we passed to the method is the insertion index. Notice that indices are zero-based, so the argument must be greater than or equal to 0 and less than or equal to len(columns).

  1. The last step is to convert the result back to a MultiIndex object.

The MultiIndex.from_frame method makes a MultiIndex from a DataFrame.

When using this approach, the level can be added at any index.

# Adding a column level to a Pandas DataFrame

You can use the pandas.MultiIndex.from_product method if you need to add a column level to a Pandas DataFrame.

main.py
import pandas as pd df = pd.DataFrame({ 'A': [1, 1, 1, 2, 3], 'B': [1, 2, 3, 4, 5] }) df.columns = pd.MultiIndex.from_product([df.columns, ['C']]) # A B # C C # 0 1 1 # 1 1 2 # 2 1 3 # 3 2 4 # 4 3 5 print(df)
The code for this article is available on GitHub

The pandas.MultiIndex.from_product() method makes a MultiIndex from the cartesian product of multiple iterables.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.