pandas 如何从另一个数据框中用一行减去数据框中的所有行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22093471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:45:08  来源:igfitidea点击:

How to subtract all rows in a dataframe with a row from another dataframe?

pythonpandasdataframe

提问by jonas

I would like to subtract all rows in a dataframe with one row from another dataframe. (Difference from one row)

我想从另一个数据帧中减去一行数据帧中的所有行。(与一排不同)

Is there an easy way to do this? Like df-df2)?

是否有捷径可寻?喜欢df-df2)?

df = pd.DataFrame(abs(np.floor(np.random.rand(3, 5)*10)),
...                 columns=['a', 'b', 'c', 'd', 'e'])
df

Out[18]:
   a  b  c  d  e
0  8  9  8  6  4
1  3  0  6  4  8
2  2  5  7  5  6


df2 = pd.DataFrame(abs(np.floor(np.random.rand(1, 5)*10)),
...                 columns=['a', 'b', 'c', 'd', 'e'])
df2

   a  b  c  d  e
0  8  1  3  7  5

Here is an output that works for the first row, however I want the remaining rows to be detracted as well...

这是适用于第一行的输出,但是我希望其余行也被减损......

df-df2

    a   b   c   d   e
0   0   8   5  -1  -1
1 NaN NaN NaN NaN NaN
2 NaN NaN NaN NaN NaN

回答by unutbu

Pandas NDFrames generally try to perform operations on items with matching indices. df - df2only performs subtraction on the first row, because the 0indexed row is the only row with an index shared in common.

Pandas NDFrames 通常尝试对具有匹配索引的项目执行操作。df - df2只对第一行进行减法运算,因为0索引行是唯一共享索引的行。

The operation you are looking for looks more like a NumPy array operation performed with "broadcasting":

您正在寻找的操作看起来更像是使用“广播”执行的 NumPy 数组操作:

In [21]: df.values-df2.values
Out[21]: 
array([[ 0,  8,  5, -1, -1],
       [-5, -1,  3, -3,  3],
       [-6,  4,  4, -2,  1]], dtype=int64)

To package the result in a DataFrame:

要将结果打包到 DataFrame 中:

In [22]: pd.DataFrame(df.values-df2.values, columns=df.columns)
Out[22]: 
   a  b  c  d  e
0  0  8  5 -1 -1
1 -5 -1  3 -3  3
2 -6  4  4 -2  1

回答by Jeff

You can do this directly in pandas as well. (I used df2 = df.loc[[0]])

您也可以直接在 Pandas 中执行此操作。(我用过df2 = df.loc[[0]]

In [80]: df.sub(df2,fill_value=0)
Out[80]: 
   a  b  c  d  e
0  0  0  0  0  0
1  7  6  0  7  8
2  4  4  3  6  2

[3 rows x 5 columns]

回答by IPV3001

Alternatively you could simply use the apply function on all rows of df.

或者,您可以简单地对 df 的所有行使用 apply 函数。

df3 = df.apply(lambda x: x-df2.squeeze(), axis=1)
# axis=1 because it should apply to rows instead of columns
# squeeze because we would like to substract Series