Python 浮点比较不';我不能以百万美元产出预期的产量
我得到了以下转换为dataframe的numpy数组:Python 浮点比较不';我不能以百万美元产出预期的产量,python,pandas,dataframe,Python,Pandas,Dataframe,我得到了以下转换为dataframe的numpy数组: data =np.array([('210', 0.5316666570181647, 0.99102639737063), ('215', 0.5541666565152505, 0.9906073078204338), ('220', 0.5658333229211469, 0.9905192216775841), ('225', 0.615
data =np.array([('210', 0.5316666570181647, 0.99102639737063),
('215', 0.5541666565152505, 0.9906073078204338),
('220', 0.5658333229211469, 0.9905192216775841),
('225', 0.6158333218035598, 0.9893290692391012),
('230', 0.10499999988824131, 0.9999143781512333),
('235', 0.061666665288309254, 0.9999999088637485),
('240', 0.061666665288309254, 0.9999999088637485),
('245', 0.061666665288309254, 0.9999999088637485)],
dtype=[('index', '|O'), ('time', '<f8'), ('min_value',
'<f8')])
df = pd.DataFrame(data)
data=np.array([('210',0.5316666570181647,0.99102639737063),
('215', 0.5541666565152505, 0.9906073078204338),
('220', 0.5658333229211469, 0.9905192216775841),
('225', 0.6158333218035598, 0.9893290692391012),
('230', 0.10499999988824131, 0.9999143781512333),
('235', 0.061666665288309254, 0.9999999088637485),
('240', 0.061666665288309254, 0.9999999088637485),
('245', 0.061666665288309254, 0.9999999088637485)],
dtype=[('index','O'),('time','查看您的数据后,很明显,造成混淆的原因是浮点数的显示方式。min\u value
中的每个值都小于1,但在显示时,其中一些值会四舍五入:
In [1131]: df
Out[1131]:
index time min_value
0 210 0.531667 0.991026
1 215 0.554167 0.990607
2 220 0.565833 0.990519
3 225 0.615833 0.989329
4 230 0.105000 0.999914
5 235 0.061667 1.000000
6 240 0.061667 1.000000
7 245 0.061667 1.000000
但是df.min_value<1
注册为所有值都小于1,因为您处理的是实际值,而不是打印的值
In [1133]: df.min_value < 1
Out[1133]:
0 True
1 True
2 True
3 True
4 True
5 True
6 True
7 True
Name: min_value, dtype: bool
这样,对取整后的数据应用过滤器,但不对实际数据进行任何更改/修改。很抱歉,这是错误的。我再次编辑帖子(minf[minf.min_value<1])@SamirAlhejaj数据框中的所有内容都小于1。您希望得到什么?查询方法也不起作用,它会以1.0的值显示行!@SamirAlhejaj您需要了解的是,数据框中的所有内容都小于1。例如,0.999999988637485
在显示时四舍五入为1.0
,但它是仍然小于1。谢谢,那么我如何以实际值(不是近似值)显示这些数字呢并删除具有最大值的行。这是我的公式。修复了设置期望值的标题。@Georgy和Azat Ibrakov,这不是打字错误。如果这是由于对浮点数学缺乏理解。请在进行密切投票前正确阅读问题,仅供参考(不是诽谤,仅供参考)这个问题也没有理由对答案投反对票,谢谢。
In [1133]: df.min_value < 1
Out[1133]:
0 True
1 True
2 True
3 True
4 True
5 True
6 True
7 True
Name: min_value, dtype: bool
In [1136]: df[np.around(df.min_value, 5) < 1]
Out[1136]:
index time min_value
0 210 0.531667 0.991026
1 215 0.554167 0.990607
2 220 0.565833 0.990519
3 225 0.615833 0.989329
4 230 0.105000 0.999914