具有多个条件的布尔索引 [英] Boolean Indexing with multiple conditions
问题描述
我有一个 Pandas DF ,我需要过滤一些行,其中包含特征'a'和特征'b'的值== 0.
I have a Pandas DF where I need to filter out some rows that contains values == 0 for feature 'a' and feature '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]
我可以看到特征'b'的0值.
I can see the 0 values for feature 'b'.
但是,如果我尝试使用OR操作数将这2个代码合并在一行代码中:
However, if I try to combine these 2 in a single line of code using the OR operand:
DF3 = DF[DF['a'] == 0 | DF['b'] == 0]
我明白了:
TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]
这是怎么回事?
推荐答案
您可以转换列'a'或'b',使其均为float64或bool.但是,保留功能数据类型的更简单的解决方案是:
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:
DF3 = DF[(DF['a'] == 0) | (DF['b'] == 0)]
一个常见的操作是使用布尔向量来过滤数据.操作员是:为或&对于and,〜不适用. 这些必须使用括号进行分组.
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.
这篇关于具有多个条件的布尔索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!