考虑第一个数据帧的2个条件来创建第二个数据帧

时间:2018-12-05 20:03:22

标签: python pandas dataframe

我有一个主要的DataFrame,我发现了一些我不想要的行。 我在下面的代码中找到了这些条件:

df.query("group == 'treatment' and landing_page != 'new_page'") 
df.query("landing_page == 'new_page' and group != 'treatment'")

现在,我希望df2考虑整个df ,上面的代码中给出的那些行。 我很难创建这个df2。有灯吗?

我的实际代码:

df2 = df.query("group == 'treatment' and landing_page == 'new_page'") and df.query("group == 'control' and landing_page == 'old_page'")

我收到此错误: ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

1 个答案:

答案 0 :(得分:1)

query更改为eval,并在索引df时反转掩码。

m1 = df.eval("group == 'treatment' and landing_page != 'new_page'") 
m2 = df.eval("landing_page == 'new_page' and group != 'treatment'")

df_out = df[~(m1 | m2)]

或者,更笼统地说,

stmts = [
    "group == 'treatment' and landing_page != 'new_page'",
    "landing_page == 'new_page' and group != 'treatment'"
]

df_out = df[~np.logical_or.reduce([df.eval(stmt) for stmt in stmts])]