我可以使用 pandas.Series.equals
比较两个 Pandas 系列是否完全相等。是否有相应的函数或参数来检查元素是否等于精度的某个 ε?
最佳答案
您可以使用 numpy.allclose
:
numpy.allclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)
Returns
True
if two arrays are element-wise equal within a tolerance.The tolerance values are positive, typically very small numbers. The relative difference (
rtol * abs(b)
) and the absolute differenceatol
are added together to compare against the absolute difference betweena
andb
.
numpy
适用于 pandas.Series
对象,因此如果您有两个对象 - s1
和 s2
,你可以简单地做:
np.allclose(s1, s2, atol=...)
atol
是您的公差值。
关于python - 比较两个 pandas 系列的 float 接近相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46554632/