
- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
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 | ||
---|---|---|---|
A | one | 1 | 2 |
two | 3 | 4 | |
three | 1 | 1 | |
B | one | 5 | 6 |
two | 7 | 8 | |
three | 2 | 2 |
X | Y | ||
---|---|---|---|
aaa | 1 | 1 | 2 |
two | 3 | 4 | |
three | 1 | 1 | |
B | 1 | 5 | 6 |
two | 7 | 8 | |
three | 2 | 2 |
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 | ||
---|---|---|---|
A | one | 1 | 2 |
two | 3 | 4 | |
three | 1 | 1 | |
B | one | 5 | 6 |
two | 7 | 8 | |
three | 2 | 2 |
col0 | col1 | ||
---|---|---|---|
A | one | 1 | 2 |
two | 3 | 4 | |
three | 1 | 1 | |
B | one | 5 | 6 |
two | 7 | 8 | |
three | 2 | 2 |
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 | ||
---|---|---|---|
A | one | 1 | 2 |
two | 3 | 4 | |
three | 1 | 1 | |
B | one | 5 | 6 |
two | 7 | 8 | |
three | 2 | 2 |
X | Y | ||
---|---|---|---|
level1 | level2 | ||
A | one | 1 | 2 |
two | 3 | 4 | |
three | 1 | 1 | |
B | one | 5 | 6 |
two | 7 | 8 | |
three | 2 | 2 |
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 | ||
---|---|---|---|
level1 | level2 | ||
A | one | 1 | 2 |
two | 3 | 4 | |
three | 1 | 1 | |
B | one | 5 | 6 |
two | 7 | 8 | |
three | 2 | 2 |
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 |