使用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)]