pandas - 如何根据另一个 df 中存在的列值从数据框中删除行?

标签 pandas

给定两个数据帧 A 和 B,它们都有“x”和“y”列,我如何才能高效删除 A 中它们的 (x, y) 对出现在 B 中的所有行。

我考虑过在 A 上使用行迭代器实现它,然后每对检查它是否存在于 B 中,但我猜这是效率最低的方法......

我尝试按照 Filter dataframe rows if value in column is in a set list of values 中的建议使用 .isin 函数但不能将它用于多列。

示例数据框:

A = pd.DataFrame([[1, 2], [1, 4], [3, 4], [2, 4]], columns=['x', 'y'])
B = pd.DataFrame([[1, 2], [3, 4]], columns=['x', 'y'])

运算后C应该包含[1,4]和[2,4]。

最佳答案

在 pandas master(或 future 的 0.13)中,isin 也将接受数据帧,但问题是它只查看每一列中的值,而不是列的精确行组合.

取自@AndyHayden 的评论(https://github.com/pydata/pandas/issues/4421#issuecomment-23052472),与集合类似的方法:

In [3]: mask = pd.Series(map(set(B.itertuples(index=False)).__contains__, A.itertuples(index=False)))
In [4]: A[~mask]
Out[4]:
   x  y
1  1  4
3  2  4

或更具可读性的版本:

set_B = set(B.itertuples(index=False))
mask = [x not in set_B for x in A.itertuples(index=False)]

与@Acorbe 的回答相比,这样做的可能优势在于它保留了 A 的索引并且不会删除 A 中的重复行(但这取决于您当然想要)。


正如我所说,0.13 将接受数据帧到 isin。但是,我认为这不会解决这个问题,因为索引也必须相同:

In [27]: A.isin(B)
Out[27]:
       x      y
0   True   True
1  False   True
2  False  False
3  False  False

您可以通过将其转换为字典来解决此问题,但现在它不会查看两列的组合,而是单独查看每一列:

In [28]: A.isin(B.to_dict(outtype='list'))
Out[28]:
       x     y
0   True  True
1   True  True
2   True  True
3  False  True

关于pandas - 如何根据另一个 df 中存在的列值从数据框中删除行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20677920/

相关文章:

python - 比较同一数据框中的两个数据框列并返回第一列中包含的文本

python - pandas 多索引中的上采样

python - 根据今天的日期和 pandas 中的其他条件过滤数据框

python - 计算最高分 - Pandas 中每个供应商的最低分

python - 值错误 : Must have equal len keys and value when setting with an iterable when inserting a list

python - 为什么 Pandas 会在 NaN 上合并?

python - 如何以特定方式旋转数据框

python-2.7 - 修改 Pandas 图中的背景颜色?

python - Pandas 数据框枢轴不适合内存

python - Pandas 按条件将 df 转换为矩阵