Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering a Pandas Dataframe with a boolean mask

How do I drop all dataframe rows that don't match a pair of conditions.

I did this:

df = df[ ! ((df['FVID'] == 0) & (df['vstDelta'] == 0)) ]

but that was a syntax error. Hopefully it illustrates what I want to do, which is to drop all records containing these 2 conditions.

like image 221
Mark Ginsburg Avatar asked Jan 23 '26 05:01

Mark Ginsburg


1 Answers

You should use '~' instead of ! to get the negation of the condition.

df = df[~((df['FVID'] == 0) & (df['vstDelta'] == 0))]
like image 180
Allen Avatar answered Jan 24 '26 18:01

Allen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!