Edamame
Edamame

Reputation: 25416

pandas: filter rows using more than one column values

I am filter out some rows of a data frame based on values of a given column like below:

new_df = my_df.loc[my_df['A_count'] == 0.0]

The code works fine. Then I tried to filter out rows using values from two columns like below:

new_df = my_df.loc[my_df['A_count'] == 0.0 & my_df['B_count'] == 0.0 ]

Then the code gave me error:

TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]

How do I actually perform the filter using the values more than one column? Thanks!

Upvotes: 1

Views: 2772

Answers (1)

piRSquared
piRSquared

Reputation: 294576

As @root pointed out... this works

new_df = my_df.loc[(my_df['A_count'] == 0.0) & (my_df['B_count'] == 0.0)]

However, you could also use query
I like query because it automatically filters for you and the query syntax is often very intuitive. Notice the lack of parenthesis.

my_df.query('A_count == 0 & B_count == 0')

Upvotes: 2

Related Questions