Victor
Victor

Reputation: 17097

Pandas filtering based on OR AND

I am trying to filter rows in a pandas df like this:

df1= df0[(df0.col1=='a' ) | (df0.col2=='b' & df0.col3=='c')]

I believe i used proper parentheses, but I get:

cannot compare a dtyped [object] array with a scalar of type [bool]

Basically, if a OR (b&C) is true is the condition i want

Upvotes: 1

Views: 66

Answers (1)

Stef
Stef

Reputation: 30579

Boolean indexing

Another common operation is the use of boolean vectors to filter the data. The operators are: | for or, & for and, and ~ for not. These must be grouped by using parentheses, since by default Python will evaluate an expression such as df.A > 2 & df.B < 3 as df.A > (2 & df.B) < 3, while the desired evaluation order is (df.A > 2) & (df.B < 3).

df1 = df0[(df0.col1=='a' ) | ((df0.col2=='b') & (df0.col3=='c'))]

Upvotes: 2

Related Questions