[英]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.