
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python – Remove a level using the name of the level and return the index
To remove a level using the name of the level and return the index, use the multiIndex.droplevel() method in Pandas. Set the name of the level to be removed as parameter.
At first, import the required libraries -
import pandas as pd
Create a multi-index. The names parameter sets the names for the levels in the index
multiIndex = pd.MultiIndex.from_arrays([[5, 10], [15, 20], [25, 30], [35, 40]], names=['a', 'b', 'c', 'd'])
Display the multi-index −
print("Multi-index...\n", multiIndex)
Dropping a level using the level name. We have passed the name of the level to be removed as a parameter −
print("\nDropping a level...\n", multiIndex.droplevel('b'))
Example
Following is the code −
import pandas as pd # Create a multi-index # The names parameter sets the names for the levels in the index multiIndex = pd.MultiIndex.from_arrays([[5, 10], [15, 20], [25, 30], [35, 40]], names=['a', 'b', 'c', 'd']) # display the multi-index print("Multi-index...\n", multiIndex) # Dropping a level using the level name # We have passed the name of the level to be removed as a parameter print("\nDropping a level...\n", multiIndex.droplevel('b'))
Output
This will produce the following output −
Multi-index... MultiIndex([( 5, 15, 25, 35),(10, 20, 30, 40)],names=['a', 'b', 'c', 'd']) Dropping a level... MultiIndex([( 5, 25, 35),(10, 30, 40)],names=['a', 'c', 'd'])
- Related Articles
- Python – Remove multiples levels using the level names and return the index
- Python Pandas - Return MultiIndex with requested level removed using the level name
- Python – Return index with a level removed
- Python - Return index with a specific level removed
- Python Pandas - Return vector of label values using level name in the MultiIndex
- Python Pandas - Set only a single new specific level using the level name in a MultiIndex
- Python Pandas - Get location and sliced index for requested label/ level but do not drop the level
- Python Pandas - Return MultiIndex with multiple levels removed using the level names
- Python – Drop a level from a multi-level column index in Pandas dataframe
- Python Pandas - Return vector of label values using integer position of the level in the MultiIndex
- Python Pandas - Convert a MultiIndex to an Index of Tuples containing the level values
- Python Pandas - Rearrange levels using level name in MultiIndex
- Python Pandas - Create a DataFrame with the levels of the MultiIndex as columns and substitute index level names
- Python Pandas - Drop the value when any level is NaN in a Multi-index
- Python Pandas - Return MultiIndex with requested level removed

Advertisements