Python 将浮点数转换为整数时的熊猫舍入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49153253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:59:54  来源:igfitidea点击:

pandas rounding when converting float to integer

pythonpandasfloating-pointintrounding

提问by NicoH

I've got a pandas DataFrame with a float (on decimal) index which I use to look up values (similar to a dictionary). As floats are not exactly the value they are supposed to be multiplied everything by 10 and converted it to integers .astype(int)before setting it as index. However this seems to do a floorinstead of rounding. Thus 1.999999999999999992 is converted to 1 instead of 2. Rounding with the pandas.DataFrame.round()method before does not avoid this problem as the values are still stored as floats.

我有一个带有浮点数(十进制)索引的 Pandas DataFrame,我用它来查找值(类似于字典)。由于浮点数不完全是它们应该将所有内容乘以 10 并.astype(int)在将其设置为索引之前将其转换为整数的值。然而,这似乎做了一个floor而不是四舍五入。因此 1.999999999999999992 被转换为 1 而不是 2。使用pandas.DataFrame.round()之前的方法四舍五入并不能避免这个问题,因为值仍然存储为浮点数。

The original idea (which obviously rises a key error) was this:

最初的想法(显然引发了一个关键错误)是这样的:

idx = np.arange(1,3,0.001)
s = pd.Series(range(2000))
s.index=idx
print(s[2.022])

trying with converting to integers:

尝试转换为整数:

idx_int = idx*1000
idx_int = idx_int.astype(int)
s.index = idx_int
for i in range(1000,3000):
    print(s[i])

the output is always a bit random as the 'real' value of an integer can be slightly above or below the wanted value. In this case the index contains two times the value 1000 and does not contain the value 2999.

输出总是有点随机,因为整数的“真实”值可能略高于或低于所需值。在这种情况下,索引包含值 1000 的两倍,但不包含值 2999。

回答by Giacomo Catenazzi

You are right, astype(int)do a conversion toward zero:

你是对的,astype(int)向零转换:

‘integer' or ‘signed': smallest signed int dtype

'integer' 或 'signed':最小的有符号 int dtype

from pandas.to_numeric documentation(which is linked from astype()for numeric conversions).

来自pandas.to_numeric 文档(链接自astype()用于数字转换)。

If you want to round, you need to do a float round, and then to convert to int:

如果要舍入,则需要进行浮点舍入,然后转换为int:

df.round(0).astype(int)

Use other rounding function, according your needs.

根据您的需要使用其他舍入函数。

回答by Matt

If I understand right you could just perform the rounding operation followed by converting it to an integer?

如果我理解正确,您可以执行舍入运算,然后将其转换为整数吗?

s1 = pd.Series([1.2,2.9])
s1 = s1.round().astype(int)

Which gives the output:

这给出了输出:

0    1
1    3
dtype: int32