我有一个主要的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().
答案 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])]