How to drop rows in Pandas DataFrame by index labels?
Last Updated :
15 Mar, 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.
Similar Reads
Pandas - How to reset index in a given DataFrame Let us see how to reset the index of a DataFrame after dropping some of the rows from the DataFrame.Approach :Â Import the Pandas module.Create a DataFrame.Drop some rows from the DataFrame using the drop() method.Reset the index of the DataFrame using the reset_index() method.Display the DataFrame
1 min read
How to Drop Rows with NaN Values in Pandas DataFrame? In Pandas missing values are represented as NaN (Not a Number) which can lead to inaccurate analyses. One common approach to handling missing data is to drop rows containing NaN values using pandas. Below are some methods that can be used:Method 1: Using dropna()The dropna() method is the most strai
2 min read
Drop Columns in DataFrame by Label Names or by Index Positions This article will discuss dropping columns in Pandas DataFrame by label names or index positions. Dropping columns can be achieved using methods like drop(), iloc[], loc[], and iterative approaches. Letâs explore these methods with examples.Drop Columns by Label Names or Index Positions Using drop()
3 min read
How to Drop Negative Values in Pandas DataFrame Handling data effectively is crucial in data analysis and manipulation. One common task is cleaning the data by removing negative values, especially when they are considered outliers or invalid entries. The Pandas library in Python offers several efficient methods to accomplish this. This article wi
3 min read
Drop specific rows from multiindex Pandas Dataframe In this article, we will learn how to drop specific rows from the multi-index DataFrame. First, let us create the multi-index DataFrame. The steps are given below: Python3 import numpy as np import pandas as pd mldx_arrays = [np.array(['lion', 'lion', 'lion', 'bison', 'bison', 'bison', 'hawk', 'hawk
3 min read