简体   繁体   English

熊猫数据框浮点精度

[英]Pandas Dataframe Float Precision

I am trying to alter my dataframe with the following line of code: 我正在尝试使用以下代码行更改数据框:

 df = df[df['P'] <= cutoff]

However, if for example I set cutoff to be 0.1, numbers such as 0.100496 make it through the filter. 但是,例如,如果我将截止设置为0.1,则数字0.100496将通过过滤器。

My suspicion is that my initial dataframe has entries in scientific notation and float format as well. 我怀疑我的初始数据框也包含科学计数法和浮点格式的条目。 Could this be affecting the rounding and precision? 这会影响舍入和精度吗? Is there a potential workaround to this issue. 是否有可能解决此问题。

Thank you in advance. 先感谢您。

EDIT: I am reading from a file. 编辑:我正在从文件中读取。 Here is a sample of the total data. 这是总数据的样本。

2.29E-98
1.81E-42
2.19E-35
3.35E-30
0.0313755
0.0313817
0.03139
0.0313991
0.0314062
0.1003476
0.1003483
0.1003487
0.1003521 
0.100496

Floating point comparison isn't perfect. 浮点比较并不完美。 For example 例如

>>> 0.10000000000000000000000000000000000001 <= 0.1
True

Have a look at numpy.isclose . 看看numpy.isclose It allows you to compare floats and set a tolerance for the comparison. 它允许您比较浮点并设置比较的公差。

Similar question here 类似的问题在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM