0

pandas version: '1.2.3', python version: 3.7.9

code:

   x = pd.DataFrame({'a':[1,2], 'b':[2,3]})
   x['a']>1 & x['b'] <= 2

it shows ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

this kind of usage seems common. pandas: multiple conditions while indexing data frame - unexpected behavior

thanks for help in advance.

richard.g
  • 3,585
  • 4
  • 16
  • 26

1 Answers1

2

If check linked answer difference is missing parentheses, because priority of operators, so add them:

mask = (x['a']>1) & (x['b'] <= 2)

df = x[mask]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252