减去两个不同大小的数据帧,但至少保持第一个数据帧的大小
Subtract two dataframes of different size, but maintain at least the size of the first dataframe
我有一个 DataFrame df,我想从那里减去 df2。
需要注意的是,我希望 df 保持相同的大小,对于 df 中的每个元素,我想减去 df2(如果 df2 中没有这样的唯一 index/column),它只是df(i,j) - 0
(因为在 df2 中找不到这样的 index/column)。
示例:
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
谢谢。
填补空白:
(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
Boud 已经为您提供了一个很好的答案,但是借助它,您也可以只提供 0 到 df.subtract
and then 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
这看起来比 (rough) 基准更快,因为我们可以避免 combine_first
组合。
%timeit df.subtract(df2, fill_value=0).reindex_like(df).astype(int)
100 loops, best of 3: <b>3.63 ms per loop</b>
%timeit (df-df2).combine_first(df).reindex_like(df).astype(int)
100 loops, best of 3: <b>8.69 ms per loop</b>
我有一个 DataFrame df,我想从那里减去 df2。
需要注意的是,我希望 df 保持相同的大小,对于 df 中的每个元素,我想减去 df2(如果 df2 中没有这样的唯一 index/column),它只是df(i,j) - 0
(因为在 df2 中找不到这样的 index/column)。
示例:
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
谢谢。
填补空白:
(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
Boud 已经为您提供了一个很好的答案,但是借助它,您也可以只提供 0 到 df.subtract
and then 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
这看起来比 (rough) 基准更快,因为我们可以避免 combine_first
组合。
%timeit df.subtract(df2, fill_value=0).reindex_like(df).astype(int)
100 loops, best of 3: <b>3.63 ms per loop</b>
%timeit (df-df2).combine_first(df).reindex_like(df).astype(int)
100 loops, best of 3: <b>8.69 ms per loop</b>