简体   繁体   English

为第一个数据帧的每一列计算两个数据帧的差异

[英]Calculating difference of two data frames for each column of first data frame

assumed I have two dataframes:假设我有两个数据框:

df1: 4 columns, n lines df1:4 列,n 行

df2: 50 columns, n lines df2:50 列,n 行

what is the best way to calculate the difference of each column of df1 to all columns of df2?计算 df1 的每一列与 df2 的所有列的差异的最佳方法是什么?

My only idea up to now is to merge the tables and create 4*50 new columns with the differences, as a loop.到目前为止,我唯一的想法是合并表并创建具有差异的 4*50 新列,作为一个循环。 But there has to be a better way, right?但必须有更好的方法,对吧?

Thanks already!已经谢谢了! Paul保罗

For this I have created 2 fictive dataframes:为此,我创建了 2 个虚构的数据框:

Input Dataframes输入数据框

df1 = pd.DataFrame({"a":[1,1,1],
                   "b":[2,2,2],
            
                  })

df2 = pd.DataFrame({"aa":[10,10,10],
                   "bb":[20,20,20],
                   "cc":[30,30,30],
                   "dd":[40,40,40],
                    "ee":[50,50,50] 
                  })
print(df1)

    a   b
0   1   2
1   1   2
2   1   2

print(df2)

    aa  bb  cc  dd  ee
0   10  20  30  40  50
1   10  20  30  40  50
2   10  20  30  40  50

Solution解决方案

df = pd.concat([df2.sub(df1[i], axis=0) for i in df1.columns],axis =1)
df.columns= [i for i in range(df1.shape[1]*df2.shape[1])]
df

Result结果

    0   1   2   3   4   5   6   7   8    9
0   9   19  29  39  49  8   18  28  38  48
1   9   19  29  39  49  8   18  28  38  48
2   9   19  29  39  49  8   18  28  38  48

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 遍历两个数据帧,并用 pandas 中的第二个数据帧的一列更新第一个数据帧的一列 - Iterate through two data frames and update a column of the first data frame with a column of the second data frame in pandas 尝试根据每个数据帧中的经纬度差异比较两个数据帧 - Trying to compare two data frames based on difference between latitude and longitude in each data frame 通过将第一个数据帧的一列与第二个数据帧的两列匹配来合并两个数据帧 - Merge two data frames by matching one column from the first data frame with two columns from the second data frame 比较具有不同列名的两个数据框,并使用来自第二个数据框的列更新第一个数据框 - Compare two data-frames with different column names and update first data-frame with the column from second data-frame 按 python 中的每一列比较两个数据帧? - Compare two data frames by each column in python? 比较第一列中两个数据框中的相同条目,并将差值移动/添加到下一列 - Compare same entry in two data frames in first column and move/add plus difference to the next column 如何将具有相同列值的两个熊猫数据框合并以形成显示值差异的第三个数据框 - How two panda data frames with same column values can be merged to form the third data frame that shows the difference of the values 添加一个新列,其中包含数据框另一列的每两行之间的差异 - Add a new column containing the difference between EACH TWO ROWS of another column of a data frame 将每个值乘以两个数据帧,然后将每个答案添加到列中 - Multiply each value in two data frames and add each answer to a column 用两个数据框计算观察的数量 - Calculating the number of observations with two data frames
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM