How to Drop Rows by Index in Pandas DataFrame

Here are two ways to drop rows by the index in Pandas DataFrame:

(1) Drop a single row by index. For example, you may use the syntax below to drop the row that has an index of 2:

df = df.drop(index=2)

 (2) Drop multiple rows by index. For instance, to drop the rows with the index values of 2, 4 and 6, use:

df = df.drop(index=[2,4,6])

Let’s see how to apply the above syntax using a practical example.

The Example

To start, let’s create a simple DataFrame with 7 rows:

import pandas as pd

data = {'Product': ['Computer','Printer','Monitor','Desk','Phone','Tablet','Scanner'],
          'Price': [900,200,300,450,150,250,150]
        }

df = pd.DataFrame(data, columns = ['Product','Price'])

print(df)

As you can see, the index values are located on the left, starting from 0:

How to Drop Rows by Index in Pandas DataFrame

Drop a Single Row by Index in Pandas DataFrame

To drop a specific row, you’ll need to specify the associated index value that represents that row.

For example, let’s drop the row with the index of 2 (for the ‘Monitor’ product). In that case, you’ll need to add the following syntax to the code:

df = df.drop(index=2)

So the complete Python code to drop the row with the index of 2 is:

import pandas as pd

data = {'Product': ['Computer','Printer','Monitor','Desk','Phone','Tablet','Scanner'],
          'Price': [900,200,300,450,150,250,150]
        }

df = pd.DataFrame(data, columns = ['Product','Price'])

df = df.drop(index=2)

print(df)

Once you run the code, you’ll no longer see the row with the index of 2:

Example

Drop Multiple Rows by Index in Pandas DataFrame

What if you want to drop multiple rows?

For instance, let’s drop the rows with the index values of 2, 4 and 6:

df = df.drop(index=[2,4,6])

Here is the complete Python code:

import pandas as pd

data = {'Product': ['Computer','Printer','Monitor','Desk','Phone','Tablet','Scanner'],
          'Price': [900,200,300,450,150,250,150]
        }

df = pd.DataFrame(data, columns = ['Product','Price'])

df = df.drop(index=[2,4,6])

print(df)

You’ll now notice that the rows with the index values of 2, 4 and 6 are no longer present:

Dropping rows Python

Index is a String

So far, you have seen how to drop rows where the index values are numeric.

By default the index values assigned to a DataFrame are numeric, starting from 0.

Alternatively, you can assign a name (string) to represent each index value.

For example, let’s replace the numeric index values with the following values:

index = ['Item_A','Item_B','Item_C','Item_D','Item_E','Item_F','Item_G']

So the DataFrame with the new index values would look as follows:

import pandas as pd

data = {'Product': ['Computer','Printer','Monitor','Desk','Phone','Tablet','Scanner'],
          'Price': [900,200,300,450,150,250,150]
        }

df = pd.DataFrame(data, columns = ['Product','Price'], index = ['Item_A','Item_B','Item_C','Item_D','Item_E','Item_F','Item_G'])

print(df)

As you can see, the new non-numeric index values would appear to the left:

How to Drop Rows by Index in Pandas DataFrame

Let’s drop two of those index values (‘Item_B’ and ‘Item_D’):

df = df.drop(index=['Item_B','Item_D'])

Here is the complete syntax:

import pandas as pd

data = {'Product': ['Computer','Printer','Monitor','Desk','Phone','Tablet','Scanner'],
          'Price': [900,200,300,450,150,250,150]
        }

df = pd.DataFrame(data, columns = ['Product','Price'], index = ['Item_A','Item_B','Item_C','Item_D','Item_E','Item_F','Item_G'])

df = df.drop(index=['Item_B','Item_D'])

print(df)

You will no longer see the rows with the index values of ‘Item_B’ and ‘Item_D’:

Drop Rows by Index in Pandas DataFrame

You can learn more about df.drop by visiting the Pandas Documentation.