How can I drop rows by index in Pandas?

How can I drop rows by index in Pandas?

Pandas is a popular library in Python used for data analysis and manipulation. One of its key functionalities is the ability to drop rows from a dataset based on their index. This can be achieved by using the “drop” function in Pandas, which takes in a list of index values as input and removes those rows from the dataset. This process is useful for removing unwanted or irrelevant data from a dataset, allowing for a more streamlined and accurate analysis. By utilizing the “drop” function in Pandas, users can easily drop rows by index and customize their dataset according to their specific needs.

Drop Rows by Index in Pandas (With Examples)


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	Nowitzki26
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	Nowitzki26
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	Nowitzki26
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

Cite this article

stats writer (2024). How can I drop rows by index in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-drop-rows-by-index-in-pandas/

stats writer. "How can I drop rows by index in Pandas?." PSYCHOLOGICAL SCALES, 30 Apr. 2024, https://scales.arabpsychology.com/stats/how-can-i-drop-rows-by-index-in-pandas/.

stats writer. "How can I drop rows by index in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-drop-rows-by-index-in-pandas/.

stats writer (2024) 'How can I drop rows by index in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-drop-rows-by-index-in-pandas/.

[1] stats writer, "How can I drop rows by index in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, April, 2024.

stats writer. How can I drop rows by index in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top