5.3 C
London
Thursday, December 19, 2024
HomePandas in PythonDataFrame Functions in PythonHow to Drop Rows by Index in Pandas (With Examples)

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

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Additional Resources

How to Drop Columns by Index in Pandas
Pandas: How to Drop Rows that Contain a Specific String
Pandas: How to Drop Duplicate Rows

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories