我贴这个我认为是答案,因为我在这里找不到类似的问题答案。在
我希望pandasround
方法将0.5舍入到1。在
>>> import pandas as pd
>>> pd.Series([0.5, 1.5, 2.5, 3.5, 4.5]).round()
0 0.0
1 2.0
2 2.0
3 4.0
4 4.0
dtype: float64
更奇怪的是:Python的round方法在Python 2.7和3.6中有不同的行为:
Python 2.7:
^{pr2}$Python 3.6:
>>> [round(x) for x in [0.5, 1.5, 2.5, 3.5, 4.5]]
[0, 2, 2, 4, 4]
这与浮点表示或我的平台(macosx)有关吗?在
这确实是由于floating point arithmetic,如Python 3documentation for ^{} 中所述,其中指出:
我相信这实际上是python中预期的行为,而不是浮点问题。看看documentation:
相关问题 更多 >
编程相关推荐