基于Pandas python中的两个条件选择数据帧的行 [英] Selecting rows of a dataframe based on two conditions in Pandas python

查看:51
本文介绍了基于Pandas python中的两个条件选择数据帧的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

选择所有在 Item Desc 列中包含X"或Y"子字符串的行.

that selects all rows that have the Item Desc column a substring of "X" or "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?

推荐答案

使用 | 而不是 or.所以:

Use | instead of or. So:

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

or 运算符想要比较两个布尔值(或两个计算结果为 True 或 False 的表达式).但是系列(或 numpy 数组)并不简单地评估为 True 或 False,在这种情况下,我们要比较两个系列的元素.为此,您可以使用称为按位或"的 |.

The or operator 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'.

Pandas 遵循 numpy 约定.请参阅此处pandas 文档中的解释.

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

这篇关于基于Pandas python中的两个条件选择数据帧的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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