比较Python中的2个不同数据帧
我有两个相同列名的数据帧。我想比较所有行 我想比较df1中的所有参数值和结果,以及df2中的相同参数和结果 多谢各位比较Python中的2个不同数据帧,python,pandas,Python,Pandas,我有两个相同列名的数据帧。我想比较所有行 我想比较df1中的所有参数值和结果,以及df2中的相同参数和结果 多谢各位 df1: Parameters Result xxx yes yyy no df2: Parameters Result xxx yes yyy no 首先需要相同顺序的参
df1:
Parameters Result
xxx yes
yyy no
df2:
Parameters Result
xxx yes
yyy no
首先需要相同顺序的
参数
和相同长度的数据帧
s,然后可以使用:
df2['new'] = np.where(df1['Result'].eq(df2['Result']), 'OK', '')
如果可能的话,顺序不同或长度不同,则使用和表示相等:
s = df2['Parameters'].map(df1.set_index('Parameters')['Result'])
df2['new'] = np.where(s.eq(df2['Result']), 'OK', '')
print (df2)
Parameters Result new
0 PubkeyAuthentication no
1 PermitRootLogin ys
2 PasswordAuthentication no
3 PermitEmptyPasswords no OK
4 X11Forwarding no OK
5 AllowUsers user OK
@stdntt-然后使用第二个解决方案。@stdntt-这与
==
或df2['new']=np相同。其中(s==df2['Result'],'OK','')
@stdntt-解决方案有问题吗?