146

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?

4 Answers 4

240

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.

https://pandas.pydata.org/docs/user_guide/indexing.html#boolean-indexing

Try:

alldata_balance = alldata[(alldata[IBRD] !=0) | (alldata[IMF] !=0)]
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, that worked great. I should have read that part of the docs.
6 years later I find myself here often... I'm a bit sad that the "natural python syntax" doeesnt work in this scenario, since I bet this trips people up all_the_time.
5

You can do like below to achieve your result:

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
....
....
#use filter with plot
#or
fg=sns.factorplot('Retailer country', data=df1[(df1['Retailer country']=='United States') | (df1['Retailer country']=='France')], kind='count')

fg.set_xlabels('Retailer country')
plt.show()


#also
#and
fg=sns.factorplot('Retailer country', data=df1[(df1['Retailer country']=='United States') & (df1['Year']=='2013')], kind='count')

fg.set_xlabels('Retailer country')
plt.show()

1 Comment

Is this an answer to the question it is posted under? If so, why are you going explaining seaborn along with it? Also, please take a look at how to format your answers
4

Just wanted to note that you can use both of or and | inside the query method:

alldata.query('IBRD!=0 or IMF!=0')

and

alldata.query('IBRD!=0 | IMF!=0')

both produce the same outcome.

Comments

4

you can separate conditions with |

#like this

df1 = df[(df['age'] > 25) | (df['gender'] == "Male")]

1 Comment

Using | was already given in the other answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.