Reputation: 864
Apologies if this has already been asked:
I want to remove all rows with values between 15-25 in one column AND have a specific string in another column.
For example:
options = ['pizza', 'pasta']
df2 = df[(~df['columnA'].between(15, 25)) & df.loc[~df['columnB'].isin(options)]]
So if a row has a value of 15-25 in columnA but does not have 'pizza' or 'pasta' in columnB, then I want that row to be retained...
Solution:
df[~((df['columnA'].between(15, 25)) & (df['columnB'].isin(options)))]
Upvotes: 1
Views: 151
Reputation: 476534
The easiest to understand is negating the entire condition, like ~((...) & (...))
:
df[~((df['columnA'].between(15, 25)) & (df['columnB'].isin(options)))]
Or you can use De Morgan's laws [wiki], and specify this as (~ ...) | (~ ...)
df[(~df['columnA'].between(15, 25)) | (~df['columnB'].isin(options))]
So the negation of x ∧ y is (¬x)∨(¬y).
Upvotes: 2