Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to subtract 2 data frame with the same size?

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

for example df1-df2 in the following:

df1:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading