python - Numpy中的小数
问题描述
我有一个看起来像这样的数据框 -
ID NoNotifications NotificationsNot total Percent of people
2544 272500 762 117 0.0
2415 2288378 256575 655 0.0
2558 192505 470610 7 0.0
我在跑步 -
for index, row in percent.iterrows():
print((117/300000)*100)
row['Percent of people'] = row['total']/(row['NoNotifications'] + row['NotificationsNot'])*100
为什么打印语句正在打印具有至少 3 个精度点的十进制值。例如,0.039
但第二条语句仅输出0.0
到Percent of people
列。
更新
我试过了,但我仍然在列中row['Percent of people'] = (655/254495300*100)
得到一堆0.0
Percent of people
更新 2
另外,试过repr((Decimal(655)/254495300*100))
了,还是一样。
解决方案
从numpy 文档-
import numpy as np
np.set_printoptions(suppress=True)
将确保“始终使用定点表示法打印浮点数,在这种情况下,当前精度中等于零的数字将打印为零”
In[2]: import numpy as np
In[3]: np.array([1/50000000])
Out[3]: array([2.e-08])
In[4]: np.set_printoptions(suppress=True)
In[5]: np.array([1/50000000])
Out[5]: array([0.00000002])
In[6]: np.set_printoptions(precision=6)
In[7]: np.array([1/50000000])
Out[7]: array([0.])
推荐阅读
- oop - 如何在 Rust 中以 OOP 样式“继承”属性?
- ssl - 无法在本地验证发行人的权限
- javascript - 如何两个知道通过谷歌登录的用户的两因素身份验证状态
- ansible - 在 playbook 中全局获取文件和变量
- powershell - 当有些是可选的时,Powershell 比较订单项
- c# - 即使编写为忽略异常,try-catch 块内的代码也会引发异常并中断执行
- javascript - 关闭响应式菜单“点击”JavaScript
- c++ - 虚拟地整理碎片内存,就好像它在 C++ 中是连续的一样
- jestjs - 如何测试使用 apollo 客户端 Reactive Var 的组件
- django - 本地建设成功 | CodeBuild AWS 的成功与失败 | 构建日志显示应用程序启动并运行 | 状态永远在进行中