[英]How to compare two data frames with same columns but different number of rows?
df1=
DF1 =
A B C D
a1 b1 c1 1
a2 b2 c2 2
a3 b3 c3 4
df2=
DF2 =
A B C D
a1 b1 c1 2
a2 b2 c2 1
I want to compare the value of the column 'D' in both dataframes. 我想比较两个数据帧中列“ D”的值。 If both dataframes had same number of rows I would just do this.
如果两个数据框的行数相同,我将这样做。
newDF = df1['D']-df2['D']
newDF = df1 ['D']-df2 ['D']
However there are times when the number of rows are different. 但是,有时行数不同。 I want a result Dataframe which shows a dataframe like this.
我想要一个显示这样的数据框的结果数据框。
resultDF=
resultDF =
A B C D_df1 D_df2 Diff
a1 b1 c1 1 2 -1
a2 b2 c2 2 1 1
EDIT: if 1st row in A,B,C from df1 and df2 is same then and only then compare 1st row of column D for each dataframe. 编辑:如果df1和df2在A,B,C中的第一行相同,则仅对每个数据帧比较D列的第一行。 Similarly, repeat for all the row.
同样,对所有行重复此操作。
Use merge
and df.eval
使用
merge
和df.eval
df1.merge(df2, on=['A','B','C'], suffixes=['_df1','_df2']).eval('Diff=D_df1 - D_df2')
Out[314]:
A B C D_df1 D_df2 Diff
0 a1 b1 c1 1 2 -1
1 a2 b2 c2 2 1 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.