Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python – Remove multiples levels using the level names and return the index
In Pandas, you can remove multiple levels from a MultiIndex using the droplevel() method. This method accepts level names or positions as parameters and returns a new index with the specified levels removed.
Syntax
MultiIndex.droplevel(level)
Parameters:
- level ? Level name(s) or position(s) to drop. Can be a single value or a list of values.
Creating a MultiIndex
First, let's create a MultiIndex with named levels ?
import pandas as pd
# Create a multi-index with named levels
multiIndex = pd.MultiIndex.from_arrays(
[[5, 10], [15, 20], [25, 30], [35, 40]],
names=['a', 'b', 'c', 'd']
)
print("Original MultiIndex:")
print(multiIndex)
Original MultiIndex:
MultiIndex([( 5, 15, 25, 35),
(10, 20, 30, 40)],
names=['a', 'b', 'c', 'd'])
Dropping Multiple Levels by Names
Remove specific levels by passing their names as a list ?
import pandas as pd
multiIndex = pd.MultiIndex.from_arrays(
[[5, 10], [15, 20], [25, 30], [35, 40]],
names=['a', 'b', 'c', 'd']
)
# Drop levels 'a' and 'd' by name
result = multiIndex.droplevel(['a', 'd'])
print("After dropping levels 'a' and 'd':")
print(result)
After dropping levels 'a' and 'd':
MultiIndex([(15, 25),
(20, 30)],
names=['b', 'c'])
Dropping Levels by Position
You can also drop levels using their numeric positions ?
import pandas as pd
multiIndex = pd.MultiIndex.from_arrays(
[[5, 10], [15, 20], [25, 30], [35, 40]],
names=['a', 'b', 'c', 'd']
)
# Drop levels at positions 0 and 3 (same as 'a' and 'd')
result = multiIndex.droplevel([0, 3])
print("After dropping levels at positions 0 and 3:")
print(result)
After dropping levels at positions 0 and 3:
MultiIndex([(15, 25),
(20, 30)],
names=['b', 'c'])
Practical Example with DataFrame
Here's how droplevel() works with a DataFrame ?
import pandas as pd
# Create DataFrame with MultiIndex
arrays = [['A', 'A', 'B', 'B'], ['X', 'Y', 'X', 'Y'], [1, 2, 1, 2]]
index = pd.MultiIndex.from_arrays(arrays, names=['first', 'second', 'third'])
df = pd.DataFrame({'values': [10, 20, 30, 40]}, index=index)
print("Original DataFrame:")
print(df)
print("\nAfter dropping 'first' and 'third' levels:")
print(df.droplevel(['first', 'third']))
Original DataFrame:
values
first second third
A X 1 10
Y 2 20
B X 1 30
Y 2 40
After dropping 'first' and 'third' levels:
values
second
X 10
Y 20
X 30
Y 40
Conclusion
Use MultiIndex.droplevel() to remove specific levels from a MultiIndex by passing level names or positions as parameters. This method returns a new index with the specified levels removed, making it useful for simplifying complex hierarchical data structures.
Advertisements
