简体   繁体   中英

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
表格1
would be the starting table.
This one Table (Table 2)
在此处输入图像描述

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?

Try

df.subtract(df2.values)

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM