Pandas filtering based on OR AND

倖福魔咒の 提交于 2021-02-05 08:03:54

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!