我有一个主DataFrame,并且我发现了一些我不想要的行。我在下面的代码中找到了这些条件:
df.query("group == 'treatment' and landing_page != 'new_page'")
df.query("landing_page == 'new_page' and group != 'treatment'")
现在,我想要一个考虑整个df
的df2
,除了上面代码中给出的那些行。我在尝试创建这个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().
发布于 2018-12-06 04:28:55
将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])]
https://stackoverflow.com/questions/53639922
复制相似问题