Reputation: 1850
I have a data frame which looks like:
import pandas as pd
import numpy as np
df = pd.DataFrame({"id": range(5), "is_bar": [np.nan, np.nan, False, True, False], "is_foo": [True, False, True, True, False]})
Now I want rows of df
where are foo, but not bar or bar is missing. In other words, this is the desired result:
id is_bar is_foo
0 0 NaN True
2 2 False True
I expected df.loc[df["is_foo"] & ~df["is_bar"]]
to work, but obviously the np.nan
s are causing TypeError
.
How can it be achieved?
Upvotes: 1
Views: 287
Reputation: 863146
I think need fillna
:
df = df.loc[df["is_foo"] & ~df["is_bar"].fillna(False)]
print (df)
id is_bar is_foo
0 0 NaN True
2 2 False True
Upvotes: 1