How to Drop Rows by Condition in Pandas DataFrame
To drop rows by condition in pandas, use boolean indexing with
df = df[condition] to keep rows that meet the condition or use df.drop() with row indices that meet the condition. This lets you filter out unwanted rows easily.Syntax
Use boolean indexing to keep rows that meet a condition:
df = df[condition]Or use drop() with indices of rows to remove:
df = df.drop(indices)Here, condition is a boolean expression on DataFrame columns, and indices are row labels to drop.
python
df = df[condition] df = df.drop(indices)
Example
This example shows how to drop rows where the column 'Age' is less than 30.
python
import pandas as pd data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Age': [25, 35, 30, 22]} df = pd.DataFrame(data) # Drop rows where Age is less than 30 condition = df['Age'] >= 30 df_filtered = df[condition] print(df_filtered)
Output
Name Age
1 Bob 35
2 Charlie 30
Common Pitfalls
One common mistake is trying to use drop() with a boolean condition directly, which does not work. Instead, you must get the indices of rows to drop.
Also, forgetting to assign the result back to the DataFrame means the original data is unchanged.
python
import pandas as pd data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Age': [25, 35, 30, 22]} df = pd.DataFrame(data) # Wrong: drop() does not accept boolean condition directly # df.drop(df['Age'] < 30) # This will raise an error # Right: get indices where condition is True indices_to_drop = df[df['Age'] < 30].index df = df.drop(indices_to_drop) print(df)
Output
Name Age
1 Bob 35
2 Charlie 30
Quick Reference
| Method | Usage | Description |
|---|---|---|
| Boolean Indexing | df = df[condition] | Keep rows where condition is True |
| Drop by Index | df = df.drop(indices) | Remove rows by their index labels |
| Get Indices by Condition | indices = df[condition].index | Find row indices matching condition |
Key Takeaways
Use boolean indexing to keep rows that meet your condition.
Use df.drop() with row indices to remove rows by condition.
Always assign the result back to your DataFrame to save changes.
You cannot pass a boolean condition directly to df.drop().
Get indices of rows to drop using df[condition].index.