You can use the following methods to drop rows based on multiple conditions in a pandas DataFrame:
Method 1: Drop Rows that Meet One of Several Conditions
df = df.loc[~((df['col1'] == 'A') | (df['col2'] > 6))]
This particular example will drop any rows where the value in col1 is equal to A or the value in col2 is greater than 6.
Method 2: Drop Rows that Meet Several Conditions
df = df.loc[~((df['col1'] == 'A') & (df['col2'] > 6))]
This particular example will drop any rows where the value in col1 is equal to A and the value in col2 is greater than 6.
The following examples show how to use each method in practice with the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'], 'pos': ['G', 'G', 'F', 'F', 'G', 'G', 'F', 'F'], 'assists': [5, 7, 7, 9, 12, 9, 3, 4], 'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]}) #view DataFrame df team pos assists rebounds 0 A G 5 11 1 A G 7 8 2 A F 7 10 3 A F 9 6 4 B G 12 6 5 B G 9 5 6 B F 3 9 7 B F 4 12
Example 1: Drop Rows that Meet One of Several Conditions
The following code shows how to drop rows in the DataFrame where the value in the team column is equal to A or the value in the assists column is greater than 6:
#drop rows where value in team column == 'A' or value in assists column > 6
df = df.loc[~((df['team'] == 'A') | (df['assists'] > 6))]
#view updated DataFrame
print(df)
team pos assists rebounds
6 B F 3 9
7 B F 4 12
Notice that any rows where the team column was equal to A or the assists column was greater than 6 have been dropped.
For this particular DataFrame, six of the rows were dropped.
Note: The | symbol represents “OR” logic in pandas.
Example 2: Drop Rows that Meet Several Conditions
The following code shows how to drop rows in the DataFrame where the value in the team column is equal to A and the value in the assists column is greater than 6:
#drop rows where value in team column == 'A' and value in assists column > 6
df = df.loc[~((df['team'] == 'A') & (df['assists'] > 6))]
#view updated DataFrame
print(df)
team pos assists rebounds
0 A G 5 11
4 B G 12 6
5 B G 9 5
6 B F 3 9
7 B F 4 12
Notice that any rows where the team column was equal to A and the assists column was greater than 6 have been dropped.
For this particular DataFrame, three of the rows were dropped.
Note: Th & symbol represents “AND” logic in pandas.
Additional Resources
The following tutorials explain how to perform other common operations in pandas:
How to Drop Rows that Contain a Specific Value in Pandas
How to Drop Rows that Contain a Specific String in Pandas
How to Drop Rows by Index in Pandas