Python Pandas - Renaming MultiIndex Labels



Renaming MultiIndex labels of a Pandas data structures is a common task, especially when working with hierarchical datasets. It involves the renaming specific labels, axis names, or index levels of the MultiIndexed objects. Pandas provides several methods to efficiently rename index labels, column labels, or index levels in MultiIndexed objects −

  • rename(): Renames specific index or column labels.

  • rename_axis(): Renames the names of the axis for the index or columns.

  • set_names(): Directly sets or changes the names of MultiIndex levels.

In this tutorial you will learn about various ways to rename labels and names of MultiIndexed data structures in Pandas.

Renaming MultiIndex Labels Using rename()

To rename the labels of the index or columns in a MultiIndexed object, you can use the pandas DataFame.rename() method. This method is useful for renaming individual labels in either the index or the columns of the pandas objects using the index and column parameters.

Example: Renaming the Specific Index Labels

Here is a basic example of using the df.rename() method to rename the specific index labels of a MultiIndexed DataFrame.

import pandas as pd

# Create a MultiIndex object
index = pd.MultiIndex.from_tuples([('A', 'one'), ('A', 'two'), ('A', 'three'),('B', 'one'), ('B', 'two'), ('B', 'three')])
# Create a DataFrame
data = [[1, 2], [3, 4], [1, 1], [5, 6], [7, 8], [2, 2]]
df = pd.DataFrame(data, index=index, columns=['X', 'Y'])

# Display the input DataFrame
print('Original MultiIndexed DataFrame:\n',df)

# Renaming specific index labels
df_renamed = df.rename(index={"A": "aaa", "one": "1"})
print("Renamed DataFrame:")
print(df_renamed)

Following is the output of the above code −

Original MultiIndexed DataFrame:
X Y
Aone12
two34
three11
Bone56
two78
three22
Renamed DataFrame:
X Y
aaa112
two34
three11
B156
two78
three22

Example: Renaming the Specific Column Labels

Following is the another example of using the df.rename() method to rename the specific column labels of a MultiIndexed DataFrame.

import pandas as pd

# Create a MultiIndex object
index = pd.MultiIndex.from_tuples([('A', 'one'), ('A', 'two'), ('A', 'three'),('B', 'one'), ('B', 'two'), ('B', 'three')])
# Create a DataFrame
data = [[1, 2], [3, 4], [1, 1], [5, 6], [7, 8], [2, 2]]
df = pd.DataFrame(data, index=index, columns=['X', 'Y'])

# Display the input DataFrame
print('Original MultiIndexed DataFrame:\n',df)

# Renaming columns
df_renamed = df.rename(columns={'X': "col0", 'Y': "col1"})
print("Renamed DataFrame:")
print(df_renamed)

Following is the output of the above code −

Original MultiIndexed DataFrame:
X Y
Aone12
two34
three11
Bone56
two78
three22
Renamed DataFrame:
col0 col1
Aone12
two34
three11
Bone56
two78
three22

Renaming the MultiIndex Axis Names

The pandas DataFrame.rename_axis() method is used to rename or set the names of the index levels in a MultiIndex. This can be particularly useful when working with multi-level indexing.

Example: Specifying/renaming the names of the index levels

This example demonstrates use of the df.rename_axis() method to rename the names of the index levels in a MultiIndexed DataFrame.

import pandas as pd

# Create a MultiIndex object
index = pd.MultiIndex.from_tuples([('A', 'one'), ('A', 'two'), ('A', 'three'),('B', 'one'), ('B', 'two'), ('B', 'three')])
# Create a DataFrame
data = [[1, 2], [3, 4], [1, 1], [5, 6], [7, 8], [2, 2]]
df = pd.DataFrame(data, index=index, columns=['X', 'Y'])

# Display the input DataFrame
print('Original MultiIndexed DataFrame:\n',df)

# Set names for the index levels
result = df.rename_axis(index=["level1", "level2"])
print("Resultant DataFrame:")
print(result)

Following is the output of the above code −

Original MultiIndexed DataFrame:
X Y
Aone12
two34
three11
Bone56
two78
three22
Renamed DataFrame:
X Y
level1level2
Aone12
two34
three11
Bone56
two78
three22

Renaming MultiIndex Levels Using set_names()

The pandas Index.set_names() method is used to rename the levels of a MultiIndex directly. This method allows you to set or change the names of individual levels in the index.

Example: Renaming the Names of the MultiIndex Levels

This example demonstrates how to change the names of a MultiIndex levels using the Index.set_names() method.

import pandas as pd

# Create a MultiIndex object
index = pd.MultiIndex.from_tuples([('A', 'one'), ('A', 'two'), ('A', 'three'),('B', 'one'), ('B', 'two'), ('B', 'three')],
names=["level0", "level1"])

# Create a DataFrame
data = [[1, 2], [3, 4], [1, 1], [5, 6], [7, 8], [2, 2]]
df = pd.DataFrame(data, index=index, columns=['X', 'Y'])

# Display the input DataFrame
print('Original MultiIndexed DataFrame:\n',df)

# Renaming a specific level
df.index= df.index.set_names("new_name", level=0)
print("Resultant DataFrame:")
print(df)

Following is the output of the above code −

Original MultiIndexed DataFrame:
X Y
level1level2
Aone12
two34
three11
Bone56
two78
three22
Resultant DataFrame:
X Y
new_namelevel2
Aone12
two34
three11
Bone56
two78
three22
Advertisements