pandas 数据框比较和浮点精度 [英] Pandas Dataframe Comparison and Floating Point Precision

查看:84
本文介绍了 pandas 数据框比较和浮点精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking to compare two dataframes which should be identical. However due to floating point precision I am being told the values don't match. I have created an example to simulate it below. How can I get the correct result so the final comparison dataframe returns true for both cells?

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  

推荐答案

好的,您可以使用

OK you can use np.isclose for this:

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

Out[250]:
array([[ True],
       [ True]], dtype=bool)

np.isclose具有相对公差和绝对公差.它们分别具有默认值:rtol=1e-05atol=1e-08

np.isclose takes relative tolerance and absolute tolerance. These have default values: rtol=1e-05, atol=1e-08 respectively

这篇关于 pandas 数据框比较和浮点精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆