Cannot perform 'rand_' with a dtyped [float64] array and scalar of type [bool]

Viewed 133

I am trying to filter a dataframe in pandas with multiple conditions and I keep getting the same error: Cannot perform 'rand_' with a dtyped [float64] array and scalar of type [bool]

I have tried several solutions such as changing the operator (or/and instead of |/&); I have reviewed the parenthesis...

Code:

df= pd.DataFrame(df)

filtered = (((df['a'] == 'str1') |
           (df['a'] == 'str2') | 
           (df['a'] == 'str3') | 
           (df['a'] == 'str4')) & 
           (df['b'] >= 13))

df= df[filtered]
1 Answers

Difficult to say exactly what is not working without more information about the dataframe.

Try the .query() method.

df.query(
  "a in ('str1', 'str2', 'str3', 'str4')").query(
  "b >= 13")
Related