Delete Pandas DataFrame row based on multiple conditions

You can delete DataFrame rows based on a condition using boolean indexing. By creating a boolean mask that selects the rows that meet the condition, you can then use the drop method to delete those rows from the DataFrame, effectively filtering out the unwanted rows. Alternatively, you can use the loc method to select the rows that you want to keep and assign them back to the DataFrame, effectively overwriting the original DataFrame with the filtered rows.

Create a Pandas DataFrame with data

import pandas as pd import numpy as np df = pd.DataFrame() df['Name'] = ['John', 'Doe', 'Bill','Jim','Harry','Ben'] df['TotalMarks'] = [82, 38, 63,22,55,40] df['Grade'] = ['A', 'E', 'B','E','C','D'] df['Promoted'] = [True, False,True,False,True,True] df
Name TotalMarks Grade Promoted 0 John 82 A True 1 Doe 38 E False 2 Bill 63 B True 3 Jim 22 E False 4 Harry 55 C True 5 Ben 40 D True

Delete rows based on condition

cont = df[ df['Promoted'] == False ].index df.drop(cont, inplace = True) df
Name TotalMarks Grade Promoted 0 John 82 A True 2 Bill 63 B True 4 Harry 55 C True 5 Ben 40 D True
**Delete all rows where Promoted is False

Delete rows based on multiple condition

cont = df[(df['Grade'] == 'E') (df['Grade'] == 'D')].index df.drop(cont, inplace = True) df
Name TotalMarks Grade Promoted 0 John 82 A True 2 Bill 63 B True 4 Harry 55 C True
** Delete all rows where Grade in 'D' or 'E'

Multiple condition on different columns

cont = df[(df['TotalMarks'] < 40 ) (df['Promoted'] == 'False')].index df.drop(cont, inplace = True) df
Name TotalMarks Grade Promoted 0 John 82 A True 2 Bill 63 B True 4 Harry 55 C True 5 Ben 40 D True
** Delete all rows where TotalMarks less than 40 and Promoted is False.

Drop entire column from Pandas DataFrame

df.drop(['Grade'], axis = 1)
Name TotalMarks Promoted 0 John 82 True 1 Doe 38 False 2 Bill 63 True 3 Jim 22 False 4 Harry 55 True 5 Ben 40 True
** Drop the Grade column from DataFrame
how to drop Pandas DataFrame row based on multiple conditions

Remove multiple column entirely from Pandas DataFrame

df.drop(['Grade','Promoted'], axis = 1)
Name TotalMarks 0 John 82 1 Doe 38 2 Bill 63 3 Jim 22 4 Harry 55 5 Ben 40
**Drop the Grade and Promoted column from DataFrame

Conclusion

To delete rows from a Pandas DataFrame based on multiple conditions, you can use logical operators like & (and) or (or) to combine multiple conditions .