Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering Pandas Dataframe using OR statement

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

but this gives me a ValueError

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

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

like image 469
Josh Avatar asked Apr 05 '15 19:04

Josh


People also ask

Which filtering method is possible on pandas?

filter() function is used to Subset rows or columns of dataframe according to labels in the specified index. Note that this routine does not filter a dataframe on its contents. The filter is applied to the labels of the index.


1 Answers

From the docs:

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

Try:

alldata_balance = alldata[(alldata[IBRD] !=0) | (alldata[IMF] !=0)] 
like image 185
Liam Foley Avatar answered Oct 12 '22 03:10

Liam Foley