2 回答

TA贡献1883条经验 获得超3个赞
使用字典理解创建助手 DataFrame 并与以下内容进行比较isin:
m = pd.DataFrame({c: ~df1[c].isin(df2[c]) for c in ['col1','col2']})
print (m)
col1 col2
0 False False
1 False True
2 True False
3 False False
4 False True
5 False True
然后numpy.where使用 mask byany测试True每行至少一个,并dot使用矩阵乘法获取列名:
df1['Error'] = np.where(m.any(axis=1),
m.dot(m.columns + ', ').str.rstrip(', ') + ' mismatch with df2',
'No mismatch with df2')
print (df1)
ID col1 col2 Error
0 1 A1 B1 No mismatch with df2
1 2 A2 B2 col2 mismatch with df2
2 3 A3 B3 col1 mismatch with df2
3 4 A4 B4 No mismatch with df2
4 5 A5 B5 col2 mismatch with df2
5 6 A6 B6 col2 mismatch with df2

TA贡献1783条经验 获得超4个赞
像这样的事情应该可以解决问题,但可能有更简单的方法。
diff = pd.concat([df1[col] == df2[col] for col in df1], axis=1)
def m(row):
mismatches = []
for col in diff.columns:
if not row[col]:
mismatches.append(col)
if mismatches == []:
return 'No mismatch'
return 'Mismatches: ' + ', '.join(mismatches)
df1['Error'] = diff.apply(m, axis=1)
添加回答
举报