问题
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
回答1:
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'))]
来源:https://stackoverflow.com/questions/57261041/pandas-filtering-based-on-or-and