基于 OR AND 的 Pandas 过滤 [英] Pandas filtering based on OR AND

查看:50
本文介绍了基于 OR AND 的 Pandas 过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试像这样过滤 Pandas df 中的行:

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]

基本上,如果 OR (b&C) 为真就是我想要的条件

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

推荐答案

布尔索引

另一个常见的操作是使用布尔向量来过滤数据.运营商是: |对于或,&为和,和〜为不.这些必须使用括号进行分组,因为默认情况下 Python 会评估一个表达式,例如 df.A > 2 &df.B<3 作为 df.A > (2 &df.B)<3,而所需的评估顺序是 (df.A > 2) &(df.B<3).

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'))]

这篇关于基于 OR AND 的 Pandas 过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆