大熊猫数据帧比较和浮点精度

PH8*_*H82 11 python pandas

我想比较两个应该相同的数据帧.但是由于浮点精度,我被告知值不匹配.我创建了一个示例来模拟它.如何获得正确的结果,以便最终比较数据帧对两个单元格都返回true?

a = pd.DataFrame({'A':[100,97.35000000001]})
b = pd.DataFrame({'A':[100,97.34999999999]})
print a

   A  
0  100.00  
1   97.35  

print b

   A  
0  100.00  
1   97.35  

print (a == b)

   A  
0  True  
1  False  
Run Code Online (Sandbox Code Playgroud)

EdC*_*ica 16

好的,你可以使用np.isclose这个:

In [250]:
np.isclose(a,b)

Out[250]:
array([[ True],
       [ True]], dtype=bool)
Run Code Online (Sandbox Code Playgroud)

np.isclose需要相对容忍度和绝对容差.这些有默认值:rtol=1e-05,atol=1e-08分别