[英]Compare two dataframes and create comparison matrix in Python?
Emp_rating_df Emp_rating_df
Emp_Id A1 A2 A3 A4
0 1001 4 3 6 7
1 1002 7 2 4 5
2 1003 3 8 2 6
3 1004 7 5 4 7
Comp_df Comp_df
Emp_Id A1 A2 A3 A4
0 1001 4 3 6 7
I need to compare two df which contains employee ratings.我需要比较两个包含员工评级的 df。
Emp_rating_df
contains employee ratings out of 10 and Comp_df
tells which employee to compare with all the employees from Emp_rating_df
. Emp_rating_df
包含员工评分(满分 10), Comp_df
告诉哪个员工与Emp_rating_df
中的所有员工进行比较。
If emp A has rating more than in any particular advantage column (A1, A2, A3, A4) then emp B then 2, if same then 1 else 0.如果 emp A 的评分高于任何特定优势列(A1、A2、A3、A4),则 emp B 则为 2,如果相同则为 1,否则为 0。
Output_df-输出_df-
Emp_Id A1 A2 A3 A4
0 1001 1 1 1 1
1 1002 0 2 2 2
2 1003 2 0 2 2
3 1004 0 0 2 1
First row would be 1 because of self comparison.由于自我比较,第一行将是 1。
You can try the below approach:您可以尝试以下方法:
First merge and filter:首先合并和过滤:
m = Emp_rating_df.merge(Comp_df,'left','Emp_Id').ffill().bfill()
a = m.filter(like='_x')
b = m.filter(like='_y')
Then assign by condition:然后按条件赋值:
cond1 = b.to_numpy() > a.to_numpy()
cond2 = b.to_numpy() == a.to_numpy()
Output = Emp_rating_df.copy()
Output[a.columns.str.split('_').str[0]] = np.select([cond1,cond2],[2,1],0)
print(Output)
Emp_Id A1 A2 A3 A4
0 1001 1 1 1 1
1 1002 0 2 2 2
2 1003 2 0 2 2
3 1004 0 0 2 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.