Renaming MultiIndex labels of a Pandas data structure is a common task, especially when working with hierarchical datasets. It involves 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:
In this tutorial by Vista Academy you will learn about various ways to rename labels and names of MultiIndexed data structures in Pandas.
rename()To rename the labels of the index or columns in a MultiIndexed object, you can use the pandas.DataFrame.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 columns parameters.
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)
Output:
Original MultiIndexed DataFrame:
X Y
A one 1 2
two 3 4
three 1 1
B one 5 6
two 7 8
three 2 2
Renamed DataFrame:
X Y
aaa 1 1 2
two 3 4
three 1 1
B 1 5 6
two 7 8
three 2 2
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)
Output:
Original MultiIndexed DataFrame:
X Y
A one 1 2
two 3 4
three 1 1
B one 5 6
two 7 8
three 2 2
Renamed DataFrame:
col0 col1
A one 1 2
two 3 4
three 1 1
B one 5 6
two 7 8
three 2 2
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.
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)
Output:
Original MultiIndexed DataFrame:
X Y
A one 1 2
two 3 4
three 1 1
B one 5 6
two 7 8
three 2 2
Resultant DataFrame:
X Y
level1 level2
A one 1 2
two 3 4
three 1 1
B one 5 6
two 7 8
three 2 2
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.
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)
Output:
Original MultiIndexed DataFrame:
X Y
level1 level2
A one 1 2
two 3 4
three 1 1
B one 5 6
two 7 8
three 2 2
Resultant DataFrame:
X Y
new_name level2
A one 1 2
two 3 4
three 1 1
B one 5 6
two 7 8
three 2 2
