首页 > 解决方案 > 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.0Percent of people列。

更新

我试过了,但我仍然在列中row['Percent of people'] = (655/254495300*100)得到一堆0.0Percent of people

更新 2

另外,试过repr((Decimal(655)/254495300*100))了,还是一样。

标签: pythonpandasnumpy

解决方案


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.])

推荐阅读