11

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

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?

2 Answers 2

13

Use | instead of or. So:

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

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 follows here the numpy conventions. See here in the pandas docs for an explanation on it.

Sign up to request clarification or add additional context in comments.

3 Comments

Would you mind pointing me to the docs for this? Is this a pandas thing or a python thing? I couldn't find anything about "|"
@joris, nice answer. Is there are more general way? e.g. if you have a list of column names e.g. ['d1', 'd2', 'd3' ...] is there a way specifing a common condition to apply to all and include the row if any of them match. In my case the cols d1 etc have either 1 or 0, and want only the rows with a 1 in any one of the cols d1 etc. There are other cols, with other data e.g. text, which I'm not considering for this selection
Not a general solution, but in that case you could do something like (df[cols] == 1).any(axis=1) to use as condition.
3

The condition should be as follows

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

Each condition has to be enclosed in parentheses as well. High priority is given for parentheses than the bitwise 'OR' operator. When the parentheses are not provided it would also give the same error

Comments

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.