Home > Mobile >  how to subtract 2 data frame with the same size?
how to subtract 2 data frame with the same size?

Time:12-10

How I can subtract values of 2 different data frame with the same size and columns?

for example df1-df2 in the following:

df1:

A  B
4  5
0  6


df2:
A  B
6  0
7  1

output:

diff:

A   B
-2  5
-7  5

Note: I have too many columns and rows, please don't suggest manually methods. no for loop please

CodePudding user response:

I guess this is what you want.

df1 = pd.DataFrame({"A": [4,0], "B": [5,6]})
df2 = pd.DataFrame({"A": [6,7], "B": [0,1]})

df = df1 - df2

df
Out[4]: 
   A  B
0 -2  5
1 -7  5
  • Related