Choosing framework strings based on two conditions in Pandas python

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

which selects all rows that have an Item Desc column, the substring "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 an error when I run it. Any help?

+6
source share
2 answers

Use | instead of or . So:

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

The or operator wants to compare two booleans (or two expressions that evaluate to True or False). But the series (or the numpy array) does not just evaluate True or False, in which case we want to compare both elements differently. For this you can use | which is called bitwise or.

Pandas follows the conventions here. See here in the pandas docs for an explanation on it.

+7
source

The condition should be as follows:

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

Each condition must also be enclosed in parentheses. High priority is given for parentheses than the bitwise operator "OR". When parentheses are not provided, this will also give the same error

+2
source

Source: https://habr.com/ru/post/1201521/


All Articles