python - 如何从 pandas python 中另一个数据帧的子集交集减去一个数据帧?

标签 python pandas dataframe subtraction

我在 python 中有以下数据框:

数据框 1

             1  2  3  4  5
dog   dog    0  1  1  0  1
      fox    1  0  0  0  0
      jumps  0  0  0  1  0
      over   1  0  1  0  1
      the    0  1  0  0  0
fox   dog    0  0  1  1  1
      fox    0  0  0  0  0
      jumps  0  0  1  0  1
      over   0  1  0  0  0
      the    0  0  0  1  1
jumps dog    0  0  0  0  0
      fox    0  1  0  1  1
      jumps  0  0  0  0  1
      over   1  0  1  0  0
      the    0  0  0  0  0
over  dog    0  0  1  0  0
      fox    0  1  0  1  1
      jumps  0  0  0  0  0
      over   0  1  0  1  0
      the    1  0  1  0  0
the   dog    0  0  1  0  0
      fox    0  0  0  0  1
      jumps  0  1  0  0  0
      over   0  0  1  1  0
      the    0  1  1  0  1

数据框 2

             1  2  4  5
dog   dog    1  0  0  0
      fox    0  1  0  1
      jumps  0  1  1  0
      the    0  0  0  0
      horse  1  0  1  0
fox   dog    0  0  0  0
      fox    0  1  0  1
      over   0  0  0  0
      the    0  1  0  1
      cat    0  0  1  0

您可以看到 dataframe2 包含 dataframe1 的多索引,但它还包含其他多索引,如 horse 和 cat。 Dataframe 2 也不包含 Dataframe 1 的所有列,因为您可以看到它缺少第 3 列。

我想从数据帧 1 中减去数据帧 2,这样函数只减去两者中共有的数据并忽略其余数据,结果数据帧的形状为数据帧 2。

有人知道 pandas 是否提供了执行此操作的内置方法,还是我需要自己构建一个函数。如果是这样,你能指出我正确的方向吗?任何建议都非常感谢。谢谢。

注意:这个问题类似于我发布的另一个问题 here除了我不想比较这些,而是​​想做减法的算术运算之外。

最佳答案

我相信你只是想要这样的东西:

In [23]: (df2 - df1.drop('3', axis=1)).fillna(df2).dropna()
Out[23]:
             1    2    4    5
dog dog    1.0 -1.0  0.0 -1.0
    fox   -1.0  1.0  0.0  1.0
    horse  1.0  0.0  1.0  0.0
    jumps  0.0  1.0  0.0  0.0
    the    0.0 -1.0  0.0  0.0
fox cat    0.0  0.0  1.0  0.0
    dog    0.0  0.0 -1.0 -1.0
    fox    0.0  1.0  0.0  1.0
    over   0.0 -1.0  0.0  0.0
    the    0.0  1.0 -1.0  0.0

Pandas 已经自动在索引上对齐,这是它神奇的一部分,但您只需要智能地填充/删除 nan。

编辑

哎呀,你实际上想要 df1 - df2,但是 df2 的形状,从那以后有点棘手 fillna(df1) 会阻止我们删除正确的行,但是,您可以只使用乘以 -1!

In [25]: (df2 - df1.drop('3', axis=1)).fillna(df2).dropna() * -1
Out[25]:
             1    2    4    5
dog dog   -1.0  1.0 -0.0  1.0
    fox    1.0 -1.0 -0.0 -1.0
    horse -1.0 -0.0 -1.0 -0.0
    jumps -0.0 -1.0 -0.0 -0.0
    the   -0.0  1.0 -0.0 -0.0
fox cat   -0.0 -0.0 -1.0 -0.0
    dog   -0.0 -0.0  1.0  1.0
    fox   -0.0 -1.0 -0.0 -1.0
    over  -0.0  1.0 -0.0 -0.0
    the   -0.0 -1.0  1.0 -0.0

或者,如果这些负零打扰了您:

In [31]: (-df2 + df1.drop('3', axis=1)).fillna(-df2).dropna()
Out[31]:
             1    2    4    5
dog dog   -1.0  1.0  0.0  1.0
    fox    1.0 -1.0  0.0 -1.0
    horse -1.0  0.0 -1.0  0.0
    jumps  0.0 -1.0  0.0  0.0
    the    0.0  1.0  0.0  0.0
fox cat    0.0  0.0 -1.0  0.0
    dog    0.0  0.0  1.0  1.0
    fox    0.0 -1.0  0.0 -1.0
    over   0.0  1.0  0.0  0.0
    the    0.0 -1.0  1.0  0.0

关于python - 如何从 pandas python 中另一个数据帧的子集交集减去一个数据帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47105162/

相关文章:

python - 如何迭代 pandas DataFrame 的列并从另一列返回值?

python - 如何将一个数据框映射到另一个数据框(python pandas)?

python - numpy 识别的特殊方法的文档位置

python - 如何使用 ctypes 编写和包装 sqlite3 所需的 python 函数作为回调?

python - 如何为 Pandas pd.read_csv 设置代理

python - 从数据框中删除特殊字符和字母数字的简单方法

python - 使用列条件处理数据框的子集

python - 将列表转换为 DataFrame 时如何处理错误 "' NoneType' object has no attribute 'keys' "

python - 哪些 PyTorch 模块受 model.eval() 和 model.train() 影响?

python - 每个纪元都覆盖剧情