In this post, we are going to discuss several approaches on how to drop rows from the Dataframe based on certain conditions applied to a column. Retain all those rows for which the applied condition on the given column evaluates to True.
We have already discussed earlier how to drop rows or columns based on their labels. However, in this post we are going to discuss several approaches on how to drop rows from the dataframe based on certain condition applied on a column. Retain all those rows for which the applied condition on the given column evaluates to True.
To download the CSV (“nba.csv” dataset) used in the code, click here.
Creating Dataframe to drop rows
In this Dataframe, currently, we are having 458 rows and 9 columns.
Python3
import pandas as pd
df = pd.read_csv('nba.csv')
print(df.head(15))
print(df.shape)
|
Output:


Delete rows based on the condition of a column
We will use vectorization to filter out such rows from the dataset which satisfy the applied condition. Let’s use the vectorization operation to filter out all those rows which satisfy the given condition.
Python3
df_filtered = df[df['Age'] >= 25]
print(df_filtered.head(15))
print(df_filtered.shape)
|
Output:
As we can see in the output, the returned Dataframe only contains those players whose age is greater than or equal to 25 years.


Delete rows based on multiple conditions on a column
As we can see in the output, the returned Dataframe only contains those players whose age is not between 20 to 25 age using df.drop().
Python3
indexAge = df[ (df['Age'] >= 20) & (df['Age'] <= 25) ].index
df.drop(indexAge , inplace=True)
df.head(15)
|
Output:

Delete rows based on multiple conditions on different columns
Here, we drop all the rows whose names and Positions are associated with ‘John Holland‘ or ‘SG’ using df.drop().
Python3
indexAge = df[ (df['Name'] == 'John Holland') | (df['Position'] == 'SG') ].index
df.drop(indexAge , inplace=True)
df.head(15)
|
Output:

RECOMMENDED ARTICLES – how to drop rows or columns based on their labels.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!