Signup/Sign In
PUBLISHED ON: MARCH 16, 2021

Pandas DataFrame droplevel() Method

In this tutorial, we will learn the Python pandas DataFrame.droplevel() method. It returns DataFrame with requested index / column level(s) removed. The below shows the syntax of the DataFrame.droplevel() method.

Syntax

DataFrame.droplevel(level, axis=0)

Parameters

level: int, str, or list-like. If a string is given, must be the name of a level. If list-like, elements must be names or positional indexes of levels.

axis:{0 or ‘index’, 1 or ‘columns’}, default 0. Axis along which the level(s) is removed:

  • 0 or ‘index’: remove level(s) in the column.

  • 1 or ‘columns’: remove level(s) in a row.

Example 1: Dropping level using the DataFrame.droplevel() Method

The DataFrame.droplevel() method drops the specified level in the DataFrame along the row axis. The below example shows the same.

import pandas as pd
df = pd.DataFrame([[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12]]).set_index([0, 1]).rename_axis(['a', 'b'])
df.columns = pd.MultiIndex.from_tuples([('c', 'e'), ('d', 'f')], names=['level_1', 'level_2'])
print(df)
print("-----After dropping the specified level------")
print(df.droplevel('a'))

Once we run the program we will get the following output.


level_1 c d
level_2 e f
a b
1 2 3 4
5 6 7 8
9 10 11 12
-----After dropping the specified level------
level_1 c d
level_2 e f
b
2 3 4
6 7 8
10 11 12

Example 2: Dropping level using the DataFrame.droplevel() Method

The DataFrame.droplevel() method drops the specified level in the DataFrame along the row axis. The below example shows the same.

import pandas as pd
df = pd.DataFrame([[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12]]).set_index([0, 1]).rename_axis(['a', 'b'])
df.columns = pd.MultiIndex.from_tuples([('c', 'e'), ('d', 'f')], names=['level_1', 'level_2'])
print(df)
print("-----After dropping the specified level------")
print(df.droplevel(1))

Once we run the program we will get the following output.


level_1 c d
level_2 e f
a b
1 2 3 4
5 6 7 8
9 10 11 12
-----After dropping the specified level------
level_1 c d
level_2 e f
a
1 3 4
5 7 8
9 11 12

Example 3: Dropping level using the DataFrame.droplevel() Method

The DataFrame.droplevel() method drops the specified level in the DataFrame along the column axis. The below example shows the same.

import pandas as pd
df = pd.DataFrame([[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12]]).set_index([0, 1]).rename_axis(['a', 'b'])
df.columns = pd.MultiIndex.from_tuples([('c', 'e'), ('d', 'f')], names=['level_1', 'level_2'])
print(df)
print("-----After dropping the specified level------")
print(df.droplevel('level_2', axis=1))

Once we run the program we will get the following output.


level_1 c d
level_2 e f
a b
1 2 3 4
5 6 7 8
9 10 11 12
-----After dropping the specified level------
level_1 c d
a b
1 2 3 4
5 6 7 8
9 10 11 12

Conclusion

In this tutorial, we learned the Python pandas DataFrame.droplevel() method. We learned the syntax, parameters and we solved examples by applying this method to the DataFrame and understood the example.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.