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 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 .
Related Topics
- Creating an empty Pandas DataFrame
- How to Check if a Pandas DataFrame is Empty
- How to check if a column exists in Pandas Dataframe
- How to delete column from pandas DataFrame
- How to select multiple columns from Pandas DataFrame
- Selecting multiple columns in a Pandas dataframe based on condition
- Selecting rows in pandas DataFrame based on conditions
- Rename column in Pandas DataFrame
- Get a List of all Column Names in Pandas DataFrame
- How to add new columns to Pandas dataframe?
- Change the order of columns in Pandas dataframe
- Concatenate two columns into a single column in pandas dataframe
- How to count the number of rows and columns in a Pandas DataFrame
- Use a list of values to select rows from a pandas dataframe
- How to iterate over rows in a DataFrame in Pandas
- How to drop rows/columns of Pandas DataFrame whose value is NaN
- How to Export Pandas DataFrame to a CSV File
- Convert list of dictionaries to a pandas DataFrame
- How to set a particular cell value in pandas DataFrame