Drop a list of rows from a Pandas DataFrame

The pandas library in Python is widely popular for representing data in tabular structures called DataFrames. When working with data analysis, you often need to remove specific rows from your DataFrame. This article demonstrates three effective methods for dropping multiple rows from a Pandas DataFrame.

Creating a Sample DataFrame

Let's start by creating a DataFrame with student marks data ?

import pandas as pd

dataset = {
    "Aman": [98, 92, 88, 90, 91], 
    "Raj": [78, 62, 90, 71, 45], 
    "Saloni": [82, 52, 95, 98, 80]
}
df = pd.DataFrame(dataset, index=["Physics", "Chemistry", "Maths", "English", "Biology"])
print(df)
           Aman  Raj  Saloni
Physics      98   78      82
Chemistry    92   62      52
Maths        88   90      95
English      90   71      98
Biology      91   45      80

Method 1: Dropping Rows by Index Position

Use numeric index positions to specify which rows to drop ?

import pandas as pd

dataset = {
    "Aman": [98, 92, 88, 90, 91], 
    "Raj": [78, 62, 90, 71, 45], 
    "Saloni": [82, 52, 95, 98, 80]
}
df = pd.DataFrame(dataset, index=["Physics", "Chemistry", "Maths", "English", "Biology"])

# Drop rows at index positions 2 and 3 (Maths and English)
df_dropped = df.drop(df.index[[2, 3]])
print("After dropping rows at positions 2 and 3:")
print(df_dropped)
After dropping rows at positions 2 and 3:
           Aman  Raj  Saloni
Physics      98   78      82
Chemistry    92   62      52
Biology      91   45      80

Method 2: Dropping Rows by Label Names

Use the actual row labels (index names) to drop specific rows ?

import pandas as pd

dataset = {
    "Aman": [98, 92, 88, 90, 91], 
    "Raj": [78, 62, 90, 71, 45], 
    "Saloni": [82, 52, 95, 98, 80]
}
df = pd.DataFrame(dataset, index=["Physics", "Chemistry", "Maths", "English", "Biology"])

# Drop rows by their label names
df_dropped = df.drop(["Maths", "English"])
print("After dropping Maths and English rows:")
print(df_dropped)
After dropping Maths and English rows:
           Aman  Raj  Saloni
Physics      98   78      82
Chemistry    92   62      52
Biology      91   45      80

Method 3: Using Index Slicing

Drop a range of consecutive rows using index slicing ?

import pandas as pd

dataset = {
    "Aman": [98, 92, 88, 90, 91], 
    "Raj": [78, 62, 90, 71, 45], 
    "Saloni": [82, 52, 95, 98, 80]
}
df = pd.DataFrame(dataset, index=["Physics", "Chemistry", "Maths", "English", "Biology"])

# Drop rows from index 2 to 3 (Maths and English)
df_dropped = df.drop(df.index[2:4])
print("After dropping rows using index slicing:")
print(df_dropped)
After dropping rows using index slicing:
           Aman  Raj  Saloni
Physics      98   78      82
Chemistry    92   62      52
Biology      91   45      80

Using the inplace Parameter

Set inplace=True to modify the original DataFrame instead of creating a new one ?

import pandas as pd

dataset = {
    "Aman": [98, 92, 88, 90, 91], 
    "Raj": [78, 62, 90, 71, 45], 
    "Saloni": [82, 52, 95, 98, 80]
}
df = pd.DataFrame(dataset, index=["Physics", "Chemistry", "Maths", "English", "Biology"])

print("Original DataFrame:")
print(df)

# Modify the original DataFrame
df.drop(["Maths", "English"], inplace=True)
print("\nAfter dropping with inplace=True:")
print(df)
Original DataFrame:
           Aman  Raj  Saloni
Physics      98   78      82
Chemistry    92   62      52
Maths        88   90      95
English      90   71      98
Biology      91   45      80

After dropping with inplace=True:
           Aman  Raj  Saloni
Physics      98   78      82
Chemistry    92   62      52
Biology      91   45      80

Comparison of Methods

Method Syntax Best For
Index Position df.drop(df.index[[2,3]]) When you know numeric positions
Label Names df.drop(["label1", "label2"]) When you know row names
Index Slicing df.drop(df.index[2:4]) Consecutive rows

Conclusion

Use drop() with label names for clarity, index positions for programmatic access, and index slicing for consecutive rows. Set inplace=True to modify the original DataFrame directly.

Updated on: 2026-03-27T06:12:00+05:30

586 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements