1

When I work with a pandas dataframe:

df = pd.DataFrame({
    'one' : [1., 2., 3., 4.],
    'two' : [4., 3., 2., 1.]})

Why does the following statement throw an error:

df['one'] > 1 & df['two'] > 2

and this doesn't:

(df['one'] > 1) & (df['two'] > 2)

Or to be more precise, why do I need brackets here?

maRtin
  • 6,336
  • 11
  • 43
  • 66
  • `&` is a bit operation for AND, so the second statement is evaluating 2 conditions and AND them (0 and 1) – depperm Jun 16 '17 at 20:16
  • 5
    `&` has a higher precedence so `1 & df['two']` is evaluated first - which throws the error. – ayhan Jun 16 '17 at 20:17

0 Answers0