How to Drop Rows by Index in Pandas (With Examples)

Dropping rows by index in Pandas can be done by using the drop function and specifying the index of the rows to be dropped. This can be done in either a single line or using a loop, depending on the number of rows to be dropped. Examples of both approaches are provided to illustrate the process. The output of the drop function should be checked to ensure that the desired rows were dropped.


You can use the following syntax to drop one row from a pandas DataFrame by index number:

#drop first row from DataFrame
df = df.drop(index=0)

And you can use the following syntax to drop multiple rows from a pandas DataFrame by index numbers:

#drop first, second, and fourth row from DataFrame
df = df.drop(index=[0, 1, 3])

If your DataFrame has strings as index values, you can simply pass the names as strings to drop:

df = df.drop(index=['first', 'second', 'third'])

The following examples show how to drop rows by index in practice.

Example 1: Drop One Row by Index

The following code shows how to drop the second row in a pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['Mavs', 'Lakers', 'Spurs', 'Cavs'],
                   'first': ['Dirk', 'Kobe', 'Tim', 'Lebron'],
                   'last': ['Nowitzki', 'Bryant', 'Duncan', 'James'],
                   'points': [26, 31, 22, 29]})

#view DataFrame
df

        team	first	last	 points
0	Mavs	Dirk	Nowitzki 26
1	Lakers	Kobe	Bryant	 31
2	Spurs	Tim	Duncan	 22
3	Cavs	Lebron	James	 29

#drop second row from DataFrame
df = df.drop(index=1) 

#view resulting dataFrame
df

        team	first	last	 points
0	Mavs	Dirk	Nowitzki 26
2	Spurs	Tim	Duncan	 22
3	Cavs	Lebron	James	 29

Example 2: Drop Multiple Rows by Index

The following code shows how to drop multiple rows in a pandas DataFrame by index:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['Mavs', 'Lakers', 'Spurs', 'Cavs'],
                   'first': ['Dirk', 'Kobe', 'Tim', 'Lebron'],
                   'last': ['Nowitzki', 'Bryant', 'Duncan', 'James'],
                   'points': [26, 31, 22, 29]})

#view DataFrame
df

        team	first	last	 points
0	Mavs	Dirk	Nowitzki 26
1	Lakers	Kobe	Bryant	 31
2	Spurs	Tim	Duncan	 22
3	Cavs	Lebron	James	 29

#drop first, second, and fourth row from DataFrame
df = df.drop(index=[0, 1, 3]) 

#view resulting dataFrame
df

	team	first	last	points
2	Spurs	Tim	Duncan	22

Example 3: Drop Rows When Index is a String

The following code shows how to drop rows from a pandas DataFrame by index when the index is a string instead of a number:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['Mavs', 'Lakers', 'Spurs', 'Cavs'],
                   'last': ['Nowitzki', 'Bryant', 'Duncan', 'James'],
                   'last': ['Nowitzki', 'Bryant', 'Duncan', 'James'],
                   'points': [26, 31, 22, 29]},
                   index=['A', 'B', 'C', 'D'])

#view DataFrame
df
        team	first	last	 points
A	Mavs	Dirk	Nowitzki 26
B	Lakers	Kobe	Bryant	 31
C	Spurs	Tim	Duncan	 22
D	Cavs	Lebron	James	 29

#remove rows with index values 'A' and 'C'
df = df.drop(index=['A', 'C'])

#view resulting DataFrame
df

team	first	last	points
B	Lakers	Kobe	Bryant	31
D	Cavs	Lebron	James	29

x