How to drop rows in Pandas DataFrame by index labels?
Last Updated :
15 Jul, 2025
Dropping rows in a Pandas DataFrame by index labels is a common operation when you need to remove specific rows based on their index positions. For example, we are working with a DataFrame that contains details of students, including their names, ages, and universities. Initially, the DataFrame has four rows, each representing a student. In this case, we want to drop the row labeled 'c'
Drop Rows in Pandas DataFrame
Using drop()
drop() method in Pandas is a simple way to remove rows by their index labels. It can drop single or multiple rows and returns a new DataFrame unless inplace=True is used to modify the original DataFrame.
Python
import pandas as pd
d = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
df = pd.DataFrame(d, index=['a', 'b', 'c'])
# Drop single row by index label
res = df.drop('b')
print(res)
print("-" * 50)
# Drop multiple rows by index labels
res = df.drop(['a', 'c'])
print(res)
print("-" * 50)
# Drop rows inplace (modifies original DataFrame)
df.drop('b', inplace=True)
print(df)
Output A B C
a 1 4 7
c 3 6 9
--------------------------------------------------
A B C
b 2 5 8
--------------------------------------------------
A B C
a 1 4 7
c 3 6 9
Explanation:
- drop('b') removes row b from the DataFrame.
- drop(['a', 'c']) removes multiple rows a and c together.
- Using inplace=True, the changes are applied directly to the original DataFrame without returning a new one.
Using Boolean indexing
Boolean indexing is the most efficient methods to filter data in a DataFrame. By applying a condition directly to a column, it returns a Boolean series that is used to select rows that meet the specified condition.
Python
import pandas as pd
# Sample DataFrame
df = pd.DataFrame({'Name': ['Ankit', 'Aishwarya', 'Ravi'],'Age': [23, 21, 25],'University': ['BHU', 'JNU', 'DU']}, index=['a', 'b', 'c'])
# Drop rows where Age is less than 23
df_filtered = df[df['Age'] >= 23]
print(df_filtered)
Output Name Age University
a Ankit 23 BHU
c Ravi 25 DU
Explanation:
- expression df['Age'] >= 23 creates a Boolean mask (True for ages 23 or above, False otherwise).
- DataFrame is filtered using this mask, keeping only rows that meet the condition.
Using query()
query() method provides a SQL-like syntax to filter DataFrame rows. This method allows you to write expressions that are evaluated directly, offering a cleaner and more readable way to filter rows compared to traditional boolean indexing, especially for complex conditions.
Python
import pandas as pd
# Sample DataFrame
d = {'Name': ['Ankit', 'Aishwarya', 'Raj', 'Simran'],'Age': [23, 21, 25, 22],'University': ['BHU', 'JNU', 'DU', 'IIT']}
df = pd.DataFrame(d)
# Drop rows where Age is less than 23 using query()
df_filtered = df.query("Age >= 23")
print(df_filtered)
Output Name Age University
0 Ankit 23 BHU
2 Raj 25 DU
Explanation:
- query("Age >= 23") filters out rows where Age is below 23.
- The condition inside the query works like an SQL WHERE clause.
Using dropna()
dropna() method is used when your DataFrame contains missing or NaN values and you want to remove rows that contain them. You can drop rows with any missing values or restrict the operation to certain columns. This method is useful when cleaning data or handling incomplete datasets.
Python
import pandas as pd
# DataFrame with NaN values
df = pd.DataFrame({'Name': ['Ankit', 'Aishwarya', None],'Age': [23, None, 25],'University': ['BHU', 'JNU', 'DU']})
# Drop rows with any NaN values
df_na = df.dropna()
print(df_na)
print("-" * 50)
# Drop rows with NaN in the 'Age' column only
res = df_na.dropna(subset=['Age'])
print(res)
Output Name Age University
0 Ankit 23.0 BHU
--------------------------------------------------
Name Age University
0 Ankit 23.0 BHU
Explanation:
- dropna() removes any row that contains a missing (NaN) value in any column.
- dropna(subset=['Age']) specifically removes rows where the Age column has missing values.
How to Drop Rows in Pandas DataFrame by Index Labels
Explore
Pandas Tutorial
4 min read
Introduction
Creating Objects
Viewing Data
Selection & Slicing
Operations
Manipulating Data
Grouping Data
Merging, Joining, Concatenating and Comparing
Working with Date and Time