Selecting Rows Of A Dataframe Based On Two Conditions In Pandas Python
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
Solution 1:
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.
Solution 2:
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
Post a Comment for "Selecting Rows Of A Dataframe Based On Two Conditions In Pandas Python"