python - 比较两个 pandas 系列的 float 接近相等?

标签 python pandas numpy floating-point equality

我可以使用 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 difference atol are added together to compare against the absolute difference between a and b.

numpy 适用于 pandas.Series 对象,因此如果您有两个对象 - s1s2,你可以简单地做:

np.allclose(s1, s2, atol=...) 

atol 是您的公差值

关于python - 比较两个 pandas 系列的 float 接近相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46554632/

相关文章:

python - keras 将两个损失与可调权重相结合,其中输出不具有相同的维度

python - 用 python 编写大型 netCDF4 文件?

python - 哪些 Python 模块为空间数据提供了良好的基础类?

python - 在 pandas 数据框中进行分组并合并 numpy 数组列

python - 合并两列,同时优先考虑第一列

python - 参数必须是字符串、类字节对象或数字,而不是 'slice'

python - 将任务分配给两台打印机的算法?

python - 用 null python pandas 替换 NaN

windows - 如何在 Windows 的 linux 子系统上安装 numpy?

python - 在 Python Pandas 中搜索数据框中的项目并将列转置为行