1

I have a data frame as follows,

import pandas as pd data = [[1, 10], [2, 20], [3, 30]] 
df = pd.DataFrame(data, columns=["a", "b"])

To query row a == 2 and b == 20, the code

df[(df["a"]==2) & (df["b"]==20)]

works fine but

df[df["a"]==2 & df["b"]==20]

returns an error:

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

Is there any underlying difference that results in this?

xzhu
  • 49
  • 5
  • 1
    One word answer: precedence. In the first case because of parentheses, `&` is only evaluated finally. In the second case `&` takes precedence. `(2 & df["b"])` doesnt make any sense. – Nihal Sangeeth Feb 13 '19 at 07:05

0 Answers0