Bitwise & and Logical and

Screen Link:
https://app.dataquest.io/m/370/working-with-missing-data/6/analyzing-correlations-in-missing-data

My Code:

v_null = (mvc[v_col].isnull() and mvc[c_col].notnull()).sum()
v_null = (mvc[v_col].isnull() & mvc[c_col].notnull()).sum()

What I expected to happen:
I expected the logical ‘and’ to work here since both the .isnull() and .notnull() functions return boolean values

What actually happened:
I get the below error when using the logical ‘and’ (1st line of code). It does work however using the Bitwise ‘&’ (2nd line of code)

But I don’t understand why it doesn’t work with the Logical ‘and’ - can anyone explain to me why this is the case?

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

It does because using & is correct and and is incorrect. When you are creating a boolean mask, pandas will only recognize & as the and operator and | as the or operators.

1 Like