Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

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 pandasDataFame.rename() method. This method is useful for renaming individual labels in either the index or the columns of the pandas objects using theindex andcolumn parameters.

Example: Renaming the Specific Index Labels

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

import pandas as pd# Create a MultiIndex objectindex = pd.MultiIndex.from_tuples([('A', 'one'), ('A', 'two'), ('A', 'three'),('B', 'one'), ('B', 'two'), ('B', 'three')])# Create a DataFramedata = [[1, 2], [3, 4], [1, 1], [5, 6], [7, 8], [2, 2]]df = pd.DataFrame(data, index=index, columns=['X', 'Y'])# Display the input DataFrameprint('Original MultiIndexed DataFrame:\n',df)# Renaming specific index labelsdf_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:
XY
Aone12
two34
three11
Bone56
two78
three22
Renamed DataFrame:
XY
aaa112
two34
three11
B156
two78
three22

Example: Renaming the Specific Column Labels

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

import pandas as pd# Create a MultiIndex objectindex = pd.MultiIndex.from_tuples([('A', 'one'), ('A', 'two'), ('A', 'three'),('B', 'one'), ('B', 'two'), ('B', 'three')])# Create a DataFramedata = [[1, 2], [3, 4], [1, 1], [5, 6], [7, 8], [2, 2]]df = pd.DataFrame(data, index=index, columns=['X', 'Y'])# Display the input DataFrameprint('Original MultiIndexed DataFrame:\n',df)# Renaming columnsdf_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:
XY
Aone12
two34
three11
Bone56
two78
three22
Renamed DataFrame:
col0col1
Aone12
two34
three11
Bone56
two78
three22

Renaming the MultiIndex Axis Names

The pandasDataFrame.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 thedf.rename_axis() method to rename the names of the index levels in a MultiIndexed DataFrame.

import pandas as pd# Create a MultiIndex objectindex = pd.MultiIndex.from_tuples([('A', 'one'), ('A', 'two'), ('A', 'three'),('B', 'one'), ('B', 'two'), ('B', 'three')])# Create a DataFramedata = [[1, 2], [3, 4], [1, 1], [5, 6], [7, 8], [2, 2]]df = pd.DataFrame(data, index=index, columns=['X', 'Y'])# Display the input DataFrameprint('Original MultiIndexed DataFrame:\n',df)# Set names for the index levelsresult = df.rename_axis(index=["level1", "level2"])print("Resultant DataFrame:")print(result)

Following is the output of the above code −

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

Renaming MultiIndex Levels Using set_names()

The pandasIndex.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 theIndex.set_names() method.

import pandas as pd# Create a MultiIndex objectindex = pd.MultiIndex.from_tuples([('A', 'one'), ('A', 'two'), ('A', 'three'),('B', 'one'), ('B', 'two'), ('B', 'three')],names=["level0", "level1"])# Create a DataFramedata = [[1, 2], [3, 4], [1, 1], [5, 6], [7, 8], [2, 2]]df = pd.DataFrame(data, index=index, columns=['X', 'Y'])# Display the input DataFrameprint('Original MultiIndexed DataFrame:\n',df)# Renaming a specific leveldf.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:
XY
level1level2
Aone12
two34
three11
Bone56
two78
three22
Resultant DataFrame:
XY
new_namelevel2
Aone12
two34
three11
Bone56
two78
three22
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp