1

enter image description here

enter image description here

Hi, I want to delete rows whose values in columns col1 and col2 are both 0.

Right now, I have:

df = df[df['col1'] != 0 and df['col2'] != 0]

But I get an error message: TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]

can anyone please explain how to implement this plz?

1 Answer 1

3

It is called boolean indexing.

You need bitwise & and add brackets for priority operators:

df = df[(df['col1'] != 0) & (df['col2'] != 0)]

Or:

df = df.query("'col1' != 0 & 'col2' != 0")
Sign up to request clarification or add additional context in comments.

1 Comment

@JunJang - Glad can help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.