使用OR语句过滤Pandas Dataframe [英] Filtering Pandas Dataframe using OR statement

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

问题描述

我有一个pandas数据框,我想根据数据框中两列的值过滤整个df.我想找回IBRD或IMF!= 0的所有行和列.

I have a pandas dataframe and I want to filter the whole df based on the value of two columns in the data frame. I want to get back all rows and columns where IBRD or IMF != 0.

alldata_balance = alldata[(alldata[IBRD] !=0) or (alldata[IMF] !=0)]

但这给了我一个ValueError

but this gives me a ValueError

ValueError:系列的真值不明确.使用a.empty, a.bool(),a.item(),a.any()或a.all().

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

所以我知道我没有正确使用or语句,有没有办法做到这一点?

So I know I am not using the or statement correctly, is there a way to do this?

推荐答案

来自文档:

另一种常见的操作是使用布尔向量来过滤 数据.操作员是:为或&对于and,〜不适用.这些 必须使用括号进行分组.

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.

http://pandas.pydata. org/pandas-docs/version/0.15.2/indexing.html#boolean-indexing

尝试:

alldata_balance = alldata[(alldata[IBRD] !=0) | (alldata[IMF] !=0)]

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

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