carguerriero
carguerriero

Reputation: 141

Numeric differences between two different dataframes in python

I would like to find the numeric difference between two or more columns of two different dataframe.

The following
Table 1
would be the starting table.
This one Table (Table 2)
enter image description here

contains the single values that I need to subtract to Table 1.

I would like to get a third table where I get the numeric differences between each row of Table 1 and the single row from Table 2. Any help?

Upvotes: 0

Views: 969

Answers (3)

Dev Khadka
Dev Khadka

Reputation: 5451

you can just do df1-df2.values like below this will use numpy broadcast to substract all df2 from all rows but df2 must have only one row

example

df1 = pd.DataFrame(np.arange(15).reshape(-1,3), columns="A B C".split())

df2 = pd.DataFrame(np.ones(3).reshape(-1,3), columns="A B C".split())

df1-df2.values

Upvotes: 1

Nidhin Bose J.
Nidhin Bose J.

Reputation: 1092

Can you try this and see if this is what you need:

import pandas as pd
df = pd.DataFrame({'A':[5, 3, 1, 2, 2], 'B':[2, 3, 4, 2, 2]})
df2 = pd.DataFrame({'A':[1], 'B':[2]})
pd.DataFrame(df.values-df2.values, columns=df.columns)
Out: 
   A  B
0  4  0
1  2  1
2  0  2
3  1  0
4  1  0

Upvotes: 1

SpghttCd
SpghttCd

Reputation: 10880

Try

df.subtract(df2.values)

with df being your starting table and df2 being Table 2.

Upvotes: 2

Related Questions