我有一个数据框,从中我选择了两个子集 dfsdf_a和df_b. 例如在iris数据集中:
df_a = iris[iris.Name == "Iris-setosa"]
df_b = iris[iris.Name == "Iris-virginica"]
让所有元素iris既不在 indf_a也不在 in的最佳方法是df_b什么?我不想参考定义df_a和的原始条件df_b。我只是假设df_aanddf_b是 的子集iris,所以我想iris根据 and 的索引df_a从中提取元素df_b。基本上,假设:
df_a = get_a_subset(iris)
df_b = get_b_subset(iris)
# retrieve the subset of iris that
# has all elements not in df_a or in df_b
# ...
编辑:这是一个看似低效且不优雅的解决方案,我相信 pandas 有更好的方法:
# get subset of iris that is not in a nor in b
df_rest = iris[map(lambda x: (x not in df_a.index) & (x not in df_b.index), iris.index)]
第二个:
df_rest = iris.ix[iris.index - df_a.index - df_b.index]
如何在熊猫中最有效/最优雅地做到这一点?谢谢。