【问题标题】:Subtract two dataframes of different size, but maintain at least the size of the first dataframe减去两个不同大小的数据帧,但至少保持第一个数据帧的大小
【发布时间】:2017-03-01 21:04:55
【问题描述】:

我有一个 DataFrame df,我想从那里减去 df2。

需要注意的是,我希望 df 保持相同的大小,并且对于 df 中的每个元素,我想减去 df2(如果 df2 中没有这样的唯一索引/列),那就是 df(i,j) - 0 (因为在 df2 中没有找到这样的索引/列)。

例子:

df:

Date    Blue    Dog Apple
1/1/2016    3   4   2
1/1/2015    3   4   2
1/1/2014    3   4   2
1/1/2013    3   4   2
1/1/2013    3   4   2
1/1/2013    3   4   2

df2:

Date    Apple   Blue    Cat
1/1/2017    1   3   2
1/1/2016    1   3   2
1/1/2015    1   3   2
1/1/2014    1   3   2

我希望 df - df2 看起来像这样:

Date    Blue    Dog Apple
1/1/2016    0   4   1
1/1/2015    0   4   1
1/1/2014    0   4   1
1/1/2013    3   4   2
1/1/2012    3   4   2
1/1/2011    3   4   2

谢谢。

【问题讨论】:

    标签: python pandas dataframe subtraction


    【解决方案1】:

    填补空白:

    (df-df2).combine_first(df).reindex_like(df).astype(int)
    Out[45]: 
              Blue  Dog  Apple
    Date                      
    1/1/2016     0    4      1
    1/1/2015     0    4      1
    1/1/2014     0    4      1
    1/1/2013     3    4      2
    1/1/2012     3    4      2
    1/1/2011     3    4      2
    

    【讨论】:

      【解决方案2】:

      Boud 已经为您提供了一个很好的答案,但是借助它,您也可以将填充值 0 提供给 df.subtract,然后提供 reindex_like

      >>> df.subtract(df2, fill_value=0).reindex_like(df).astype(int)
                Blue  Dog  Apple
      Date                      
      1/1/2016     0    4      1
      1/1/2015     0    4      1
      1/1/2014     0    4      1
      1/1/2013     3    4      2
      1/1/2012     3    4      2
      1/1/2011     3    4      2
      

      这看起来比(粗略)基准测试更快,因为我们可以避免combine_first 组合。

      %timeit df.subtract(df2, fill_value=0).reindex_like(df).astype(int)
      100 loops, best of 3: 3.63 ms per loop
      
      %timeit (df-df2).combine_first(df).reindex_like(df).astype(int)
      100 loops, best of 3: 8.69 ms per loop

      【讨论】:

        猜你喜欢
        • 2023-02-02
        • 2017-04-20
        • 2019-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多