【问题标题】:Round pandas data frame/series圆形熊猫数据框/系列
【发布时间】:2015-11-30 16:13:01
【问题描述】:

我在 pandas 数据框中有一列看起来像这样(更长,但这里是前几行):

>df_fill['col1']

0      5987.8866699999998672865
1     52215.5966699999989941716
2       201.8966700000000003001
3         3.8199999999999998401

我想将整列四舍五入到小数点后 5 位。我可以将其四舍五入为整数,但不能四舍五入到小数点后的任何位数。列的类型是浮点数。

> np.around(df_fill['col1'], 0)

0      5988
1     52216
2       202
3         4

> np.around(df_fill['col1'], 5)

0      5987.8866699999998672865
1     52215.5966699999989941716
2       201.8966700000000003001
3         3.8199999999999998401

> (df_fill['col1']).round()

0      5988
1     52216
2       202
3         4

>(df_fill['col1']).round(5)

0      5987.8866699999998672865
1     52215.5966699999989941716
2       201.8966700000000003001
3         3.8199999999999998401

> (df_fill['col1']).round(decimals=5)

0      5987.8866699999998672865
1     52215.5966699999989941716
2       201.8966700000000003001
3         3.8199999999999998401

> str((df_fill['col1']).round(decimals=5))
'0      5987.8866699999998672865\n1     52215.5966699999989941716\n2       201.8966700000000003001\n3         3.8199999999999998401\

我在这里缺少什么?

【问题讨论】:

  • df_fill['col1'].round(5) 不能正常工作吗?
  • 什么是df_fill['col1'].dtype
  • @EdChum 没有用,尝试我仍然得到小数点后 19 位的结果。
  • unutbu 有一点,这里的dtype是什么?
  • @unutbu dtype('float64')

标签: python pandas dataframe


【解决方案1】:

浮动can only represent a subset of the real numbers。它只能准确地表示那些是两个负幂之和的小数(“二进制分数”)。 将浮点数舍入到 5 位后,新的浮点数可能不是具有 5 位小数的实数,因为小数部分可能无法准确表示为二进制小数。相反,舍入返回最接近该实数的浮点数

如果你设置了

pd.options.display.float_format = '{:.23g}'.format

那么 Pandas 将在浮点数的字符串表示中显示最多 23 位数字:

import pandas as pd

pd.options.display.float_format = '{:.23g}'.format

df_fill = pd.DataFrame({'col1':[ 5987.8866699999998672865, 52215.5966699999989941716, 
                                201.8966700000000003001, 3.8199999999999998401]})

#                       col1
# 0 5987.8866699999998672865
# 1 52215.596669999998994172
# 2 201.89667000000000030013
# 3 3.8199999999999998401279

print(df_fill['col1'].round(5))
# 0   5987.8866699999998672865
# 1   52215.596669999998994172
# 2   201.89667000000000030013
# 3   3.8199999999999998401279
# Name: col1, dtype: float64

但如果你将 float_format 设置为显示 5 个十进制数字:

pd.options.display.float_format = '{:.5f}'.format

然后

print(df_fill['col1'].round(5))

产量

0    5987.88667
1   52215.59667
2     201.89667
3       3.82000
Name: col1, dtype: float64

注意底层浮动没有改变;只是它的显示方式。

【讨论】:

  • 是的,就是这样。对此仍然有点困惑,但我通过取两排排在第 5 位,然后不相等来确认。对它们进行四舍五入,虽然它们仍然显示完整的数字,但四舍五入的值是“相等的”。我将显示更改为 5 只是为了让我放心。谢谢!
  • 注意在 0.17.0 中,也会有一个 DataFrame.round() 方法
【解决方案2】:

您的问题是由于表示浮点数的精度问题。数字 5987.88667 不能用浮点数精确表示,可以表示的最接近的数字是 5987.8866699999998672865。因此,您已经拥有最接近数组中所需数字的数字,因此将其四舍五入到小数点后 5 位将无效。您已经有正确的调用:

(df_fill['col1']).round(5)

如果您尝试四舍五入到小数点后 2 位,您会发现它有效。所以我建议你不要担心。如果问题是数字在屏幕上的显示方式,那么您可以将数字打印到正确的小数位数的字符串中:

print "%.5f"%(df_fill['col1'])

【讨论】:

    猜你喜欢
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 2022-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多