根据df中列值的多个条件过滤df?

时间:2020-09-26 23:28:20

标签: python python-3.x pandas

我的df为:

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

我只想为状态为False,id_reference存在于id列,状态为True且ids_related为NaN的行过滤df

所以预期的输出将是

id | status | id_reference | ids_related
 4    False       1              NaN

我有类似的代码

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

这为我提供了状态为True且id引用存在于id列为false的行,但我想对其进行调整以查看ids_related列是否为我们要过滤的列的NaN 谢谢!

1 个答案:

答案 0 :(得分:1)

逐步

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

或链式解决方案;

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