接近 0.05 的舍入从结果中删除一位

Reu*_*eut 5 python rounding pandas

我有两列带有数字数据的熊猫表(dtype flaot64)。我已经将每列四舍五入到小数点后有 2 位数字,然后使用函数将其四舍五入到接近 0.5,但由于某种原因,只有一列四舍五入为 0.05,第二列四舍五入但错过了第二位数字。

这是一个假的例子,它可以工作并显示流程:

table=pd.DataFrame({'A': [0.62435, 0.542345,0.213452],
                   'B': [0.22426,0.15779,0.30346]})

#function for round to near 0.5:
def custom_round(x, base=5):
    return base * round(float(x)/base)

table['A'] = table['A'].astype(float).round(2).apply(lambda x: custom_round(x, base=.05))
table['B'] = table['B'].astype(float).round(2).apply(lambda x: custom_round(x, base=.05))
table

>>>

A   B
0   0.60    0.20
1   0.55    0.15
2   0.20    0.30
Run Code Online (Sandbox Code Playgroud)

但在我的桌子上,我最终得到了:

在此处输入图片说明

当我在没有函数的情况下运行脚本接近 0.5 时,我仍然得到两位数:

table['B'] = table['B'].round(2)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我的问题是为什么会这样?以及如何修复它以便将两列四舍五入为 0.05 并显示两个数字?

编辑:有人问我如何将它应用到我的真实桌子上,所以:

df['A'] = df['A'].astype(float).round(2).apply(lambda x: custom_round(x, base=.05))
df['B']= df['B'].round(2).apply(lambda x: custom_round(x, base=.05))
Run Code Online (Sandbox Code Playgroud)

np8*_*np8 4

您的数字已正确舍入。下面我来解释一下,

  1. 如何显示2位精度?
  2. 示例数据发生了什么?

1.如何显示2位精度?

如果您确实只想显示两位数字,则可以custom_round完全跳过舍入函数(),并在打印数据帧之前运行此*:

pd.options.display.float_format = '{:,.2f}'.format
Run Code Online (Sandbox Code Playgroud)

这将使浮点值数据以 2 位精度打印。例子:

table=pd.DataFrame({'A': [0.62435, 0.542345,0.213452],
                   'B': [0.22426,0.18779,0.30346]})
In [1]: table
Out[1]:
     A    B
0 0.62 0.22
1 0.54 0.19
2 0.21 0.30
Run Code Online (Sandbox Code Playgroud)

2. 示例数据发生了什么?

  • 使用问题中给出的相同数据
table=pd.DataFrame({'A': [0.62435, 0.542345,0.213452],
                   'B': [0.22426,0.15779,0.30346]})

# execute code with custom_round in the question

In [1]: table
Out[1]:
      A     B
0  0.60  0.20
1  0.55  0.15
2  0.20  0.30
Run Code Online (Sandbox Code Playgroud)
  • 将 B 的中间值设置为 0.18779(四舍五入为 0.20)
table=pd.DataFrame({'A': [0.62435, 0.542345,0.213452],
                   'B': [0.22426,0.18779,0.30346]})

# execute code with custom_round in the question

In [1]: table
Out[1]:
      A    B
0  0.60  0.2
1  0.55  0.2
2  0.20  0.3
Run Code Online (Sandbox Code Playgroud)

为什么会出现这种情况?

在内部,该数字四舍五入为两位数精度。当您将表格打印到控制台/Jupyter 笔记本时,如果最后一个值(第二位数字)全为零,则 pandas 会跳过打印。因此,数据的精度为两位数(例如 0.20),但仅以一位数精度显示,因为 0.20 = 0.2。


* 您还可以使用其他打印方案:pd.options.display.float_format可以设置为任何可调用的

[...]接受浮点数并返回 具有所需数字格式的字符串。这在某些地方使用,例如 SeriesFormatter。有关示例,请参阅 core.format.EngFormatter。