Last updated: Apr 12, 2024
Reading time·5 min

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.
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)

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.
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.
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)
If you pass a mapping to the pandas.concat() method, the sorted keys are used
as the keys argument.
If you need to add a level to the columns of a MultiIndex, set the axis
parameter to 1.
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)

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.
DataFrameYou can also add a level to a MultiIndex by converting the index to a
DataFrame.
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)

DataFrame.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).
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.
You can use the pandas.MultiIndex.from_product method if you need to add a
column level to a Pandas DataFrame.
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 pandas.MultiIndex.from_product() method makes a MultiIndex from the cartesian product of multiple iterables.
You can learn more about the related topics by checking out the following tutorials: