gpt4 book ai didi

python - 比较 Python Pandas 中的 2 个不同数据帧

转载 作者:行者123 更新时间:2023-12-04 13:36:44 28 4
gpt4 key购买 nike

我有 2 个具有相同列名的数据框。我想将所有行相互比较。

我想比较df1中的所有参数值和结果,df2中的参数和结果相同。

谢谢你。

df1:
Parameters Result
xxx yes
yyy no


df2:
Parameters Result
xxx yes
yyy no

最佳答案

首先是 Parameters 的必要相同顺序并且两者长度相同 DataFrame s,那么可以使用:

df2['new'] = np.where(df1['Result'].eq(df2['Result']), 'OK', '')

如果可能的顺序不同或长度不同,请使用 Series.map numpy.where Series.eq 对于平等:
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

关于python - 比较 Python Pandas 中的 2 个不同数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61425156/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com