I'm working with a some Pandas dataframes and I can't quite get why some boolean operators are allowed and work in the .loc-selector and others give an error. To be precise, let's take the following dataframe:
import pandas as pd
df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(),
'B': 'one one two thr two two one thr'.split()})
Now both 'two' == 'two' and 'w' in 'two' evaluate as True, but when used with df.loc[...] the following works:
df.loc[df['B'] == 'two']
printing out
A B
2 foo two
4 foo two
5 bar two
But the following raises a KeyError: False -error.
df.loc['w' in df['B']]
I know ways to work around this, but none of them feel particularly smooth, and even worse I don't understand at all why the 'w' in df['B'] -selector is not allowed in .loc.