Chris90
Chris90

Reputation: 1998

Filter df based on multiple conditions of values of columns in the df?

I have df below as:

id   status   id_reference   ids_related
1    True        NaN            4
4    False       1              NaN
2    False       NaN            NaN
7    False       3              11,12
6    True        2              NaN
10   True        4              NaN
22   True        1              NaN
11   True        7              NaN
12   True        7              NaN

I want to filter df for only rows where status is False, id_reference exists in id column whos status is True and ids_related is NaN

so expected output would be

id | status | id_reference | ids_related
 4    False       1              NaN

I have code like

(df.loc[df["status"]&df["id_reference"].astype(float).isin(df.loc[~df["status"], "id"])])

This gives me rows where status is True and id reference exists in id column where it is false, but I want to tweak this to also look at ids_related column is NaN for the column that we are filtering for Thanks!

Upvotes: 0

Views: 34

Answers (1)

wwnde
wwnde

Reputation: 26676

Step by step

 g=df[~df.status]#g=df[~df.status.astype(bool)]
    g[(g.ids_related.isna())&(g.id_reference.eq('1'))]

Or chained solution;

df[((~df.status)&(df.ids_related.isna())&(df.id_reference.eq('1')))]

Upvotes: 1

Related Questions