Handing the pandas.drop function a list of True
and False
statements drops the first two lines. Why? Is this a bug?
df = pd.DataFrame({
No, this is not a bug, it is just a side-effect of that True
and False
are equal to 1
and 0
This code:
df = pd.DataFrame({"foo":[1,2,3]})
df.drop([False, False, True])
Is identical to this code:
df = pd.DataFrame({"foo":[1,2,3]})
df.drop([0, 0, 1])
The pandas drop function takes a list of indicies to drop, not a mask.
The proper way of using masks to drop data is either to mask, then access the index and hand this to the drop function:
df.drop(df[[False, False, True]].index)
foo
0 1
1 2
Or just by inverted masking:
df[~pd.Series([False, False, True])]
foo
0 1
1 2