Signup/Sign In

Pandas DataFrame drop() Method

In this tutorial, we will learn the python pandas DataFrame.drop() method. It drops specified labels from rows or columns. It removes rows or columns by specifying label names and corresponding axis, or by specifying directly index or column names. When using a multi-index, labels on different levels can be removed by specifying the level.

  • It returns the DataFrame or None. DataFrame without the removed index or column labels or None if inplace=True.

  • It raises KeyError exception if any of the labels are not found in the selected axis.

The below shows the syntax of the DataFrame.drop() method.

Syntax

DataFrame.drop(labels=None,axis=0,index=None,columns=None,level=None,inplace=False,errors='raise')

Parameters

labels: single label or list-like. Index or column labels to drop.

axis: {0 or ‘index’, 1 or ‘columns’}, default 0. Whether to drop labels from the index (0 or ‘index’) or columns (1 or ‘columns’).

index: single label or list-like. Alternative to specifying axis (labels, axis=0 is equivalent to index=labels).

columns: single label or list-like. Alternative to specifying axis (labels, axis=1 is equivalent to columns=labels).

level: int or level name, optional. For MultiIndex, the level from which the labels will be removed.

inplace: bool, default False. If False, return a copy. Otherwise, do operation inplace and return None.

errors: {‘ignore’, ‘raise’}, default ‘raise’. If ‘ignore’, suppress error and only existing labels are dropped.

Example 1: Drop rows using the DataFrame.drop() Method

The DataFrame.drop() method drops the specified label from the DataFrame along the row axis. The below example shows the same.

import pandas as pd
df=pd.DataFrame([[0,1,2,3], [4,5,6,7],[8,9,10,11]],columns=('a','b','c','d'))
print("------DataFrame-------")
print(df)
print("------After dropping a specific label from the row of the DataFrame-------")
print(df.drop(1))

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


-----DataFrame-------
a b c d
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
------After dropping a specific label from the row of the DataFrame-------
a b c d
0 0 1 2 3
2 8 9 10 11

Example 2: Drop rows using the DataFrame.drop() Method

The DataFrame.drop() method drops the specified label from the DataFrame by specifying the axis alternatively index=1. The below example shows the same.

import pandas as pd
df=pd.DataFrame([[0,1,2,3], [4,5,6,7],[8,9,10,11]],columns=('a','b','c','d'))
print("------DataFrame-------")
print(df)
print("------------After dropping a specific label from the row of the DataFrame---------")
print(df.drop(index=1))

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


------DataFrame-------
a b c d
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
------------After dropping a specific label from the row of the DataFrame---------
a b c d
0 0 1 2 3
2 8 9 10 11

Example 3: Drop rows using the DataFrame.drop() Method

The DataFrame.drop() method drops the specified label from the DataFrame along the column axis. The below example shows the same.

import pandas as pd
df=pd.DataFrame([[0,1,2,3], [4,5,6,7],[8,9,10,11]],columns=('a','b','c','d'))
print("------DataFrame-------")
print(df)
print("------After dropping a specific label from the column of the DataFrame-------")
print(df.drop('b',axis=1))

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


------DataFrame-------
a b c d
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
------After dropping a specific label from the column of the DataFrame-------
a c d
0 0 2 3
1 4 6 7
2 8 10 11

Example 4: Drop rows using the DataFrame.drop() Method

The DataFrame.drop() method drops the specified label from the DataFrame by specifying the axis alternatively column=’b’. The below example shows the same.

import pandas as pd
df=pd.DataFrame([[0,1,2,3], [4,5,6,7],[8,9,10,11]],columns=('a','b','c','d'))
print("------DataFrame-------")
print(df)
print("------After dropping a specific label from the column of the DataFrame-------")
print(df.drop(columns='b'))

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


------DataFrame-------
a b c d
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
------After dropping a specific label from the column of the DataFrame-------
a c d
0 0 2 3
1 4 6 7
2 8 10 11

Example 5: Drop rows using the DataFrame.drop() Method

The DataFrame.drop() method raises KeyError if any of the labels is not found in the selected axis.

import pandas as pd
df=pd.DataFrame([[0,1,2,3], [4,5,6,7],[8,9,10,11]],columns=('a','b','c','d'))
print("------After dropping a specific label from the row of the DataFrame-------")
print(df.drop(5))

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


------After dropping a specific label from the row of the DataFrame-------
KeyError: '[5] not found in axis'

Conclusion

In this tutorial, we learned the python pandas DataFrame.drop() method. We learned the syntax, parameters and solved examples by passing different parameters to the method.



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.