I’m wondering why the below conditional selection isn’t working. I would expect indices 0 and 3 to be selected but this is returning nothing. Wondering if I’m missing something obvious.
In [5]: a = {'A':['this', 'is', 'an', 'example'], 'B':[None, None, None, None],
...: 'C':['some', 'more', 'example', 'data']}
In [6]: df = pd.DataFrame(a)
In [7]: df
Out[7]:
A B C
0 this None some
1 is None more
2 an None example
3 example None data
This returns 2 rows:
In [8]: df.loc[(df['A'].str.len() > 3)]
Out[8]:
A B C
0 this None some
3 example None data
And this returns all rows:
In [9]: df.loc[(df['B'].isnull())]
Out[9]:
A B C
0 this None some
1 is None more
2 an None example
3 example None data
So I would expect this to return indices 0 and 3 but it doesn't return any rows
In [10]: df.loc[(df['B'].isnull() & df['A'].str.len() > 3)]
Out[10]:
Empty DataFrame
Columns: [A, B, C]
Index: []
Any help would be appreciated.
Thanks!
Series/DataFramelogical comparison operators can help cut down on all of the parentheses:df.loc[df['B'].isnull() & df['A'].str.len().gt(3)]