I am experimenting with the Pandas loc()
method, used with boolean arrays as arguments.
I created a small dataframe to play with:
col1 col2 col3 col4
0 a 1 2 3
1 b NaN NaN 6
2 c NaN 8 9
3 d NaN 11 12
4 e 13 14 15
5 f 17 18 19
6 g 21 2 2 23
And a boolean array to use on axis 1 to subset a number of columns:
a1 = pd.Series([True, False, True, False])
I then tried:
df.loc[: , a1]
I got an error message:
IndexingError: Unalignable boolean Series key provided
How can I apply the boolean array to subset a number of columns with loc()
?