How to subtract all rows in a dataframe with a row from another dataframe?
Pandas NDFrames generally try to perform operations on items with matching indices. df - df2
only performs subtraction on the first row, because the 0
indexed row is the only row with an index shared in common.
The operation you are looking for looks more like a NumPy array operation performed with "broadcasting":
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:
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
You can do this directly in pandas as well. (I used 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]