Python 具有多个条件的布尔索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34531416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:07:40  来源:igfitidea点击:

Boolean Indexing with multiple conditions

pythonpandas

提问by user5730994

I have a Pandas DFwhere I need to filterout some rows that contains values == 0 for feature 'a' and feature 'b'.

我有一个Pandas DF,我需要过滤掉一些包含值 == 0 的特征“a”和特征“b”的行。

In order to inspect the values, I run the following:

为了检查这些值,我运行以下命令:

DF1 = DF[DF['a'] == 0]

Which returns the right values. Similarly, by doing this:

它返回正确的值。同样,通过这样做:

DF2 = DF[DF['b'] == 0]

I can see the 0 values for feature 'b'.

我可以看到特征“b”的 0 值。

However, if I try to combine these 2 in a single line of code using the OR operand:

但是,如果我尝试使用 OR 操作数将这两个组合在一行代码中:

DF3 = DF[DF['a'] == 0 |  DF['b'] == 0]

I get this:

我明白了:

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

What's happening here?

这里发生了什么事?

采纳答案by Luis Miguel

You can transform either column 'a' or 'b' so they are both either float64 or bool. However, an easier solution that preserves the data type of your features is this:

您可以转换列 'a' 或 'b',使它们都是 float64 或 bool。但是,保留要素数据类型的更简单的解决方案是:

DF3 = DF[(DF['a'] == 0) | (DF['b'] == 0)]

A 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.

一个常见的操作是使用布尔向量来过滤数据。运营商是: | for or, & for and, and ~ for not. 这些必须使用括号进行分组。