pandas 基于Pandas python中的两个条件选择数据帧的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25597196/
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-09-13 22:24:40  来源:igfitidea点击:

Selecting rows of a dataframe based on two conditions in Pandas python

pythonpandas

提问by wolfsatthedoor

I have a df, and I want to run something like:

我有一个 df,我想运行类似的东西:

subsetdf= df.loc[(df['Item_Desc'].str.contains('X')==True) or \
                 (df['Item_Desc'].str.contains('Y')==True ),:]

that selects all rows that have the Item Desc column a substring of "X" or "Y".

选择所有具有“项目描述”列的“X”或“Y”子字符串的行。

The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). 

I get the error when I run that. Any help?

当我运行它时出现错误。有什么帮助吗?

回答by joris

Use |instead of or. So:

使用|代替or。所以:

df.loc[(cond1) | (cond2), :]

The oroperator wants to compare two boolean values (or two expression that evaluate to True or False). But a Series (or numpy array) does not simply evaluates to True or False, and in this case we want to compare both series element-wise. For this you can use |which is called 'bitwise or'.

or运营商希望比较两个布尔值(或两个表达式计算结果为真或假)。但是系列(或 numpy 数组)并不简单地评估为 True 或 False,在这种情况下,我们要比较两个系列的元素。为此,您可以使用|称为“按位或”的方法。

Pandas follows here the numpy conventions. See herein the pandas docs for an explanation on it.

Pandas 遵循 numpy 约定。有关它的解释,请参见pandas 文档中的此处

回答by Praveen Gupta Sanka

The condition should be as follows

条件应如下

df.loc[(cond1) | (cond2)]

Each condition has to be enclosed in parentheses as well. High priority is given for parentheses than the bitwise 'OR' operator. When the parentheses are not provided it would also give the same error

每个条件也必须括在括号中。括号的优先级高于按位“或”运算符。当没有提供括号时,它也会给出同样的错误