user3451026 user3451026 - 12 days ago 6
Python Question

Using two conditions inside pandas dataframe filter

I'm looking to filter out any rows where two columns values are equal to 0 and 1. The following was my attempt at this but hasn't yielded any results.

x = x[x.value_1 != 0 and x.value_2 !=1]


Running this returns the following error:


ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().


I know it is possible to filter using either one of these two conditions alone. However, it doesn't seem to like when I use them together. Any advice on a quick fix that doesn't include merging? Thanks!

Answer

Try this if you want anything except when value_1 is 0 and value_2 is 1:

x = x[~((x.value_1 == 0) & (x.value_2 == 1))]
Comments