【发布时间】:2015-12-15 13:54:02
【问题描述】:
我想了解为什么在同一个 DF 上使用 1) np.round 和 2) applymap 时会得到不同的值
我的朋友
df1 = pd.DataFrame({'total': [25.23, 3.55, 76.55, 36.48, 45.59]}, index=['cat1', 'cat2', 'cat3', 'cat4', 'cat5'])
total
cat1 25.23
cat2 3.55
cat3 76.55
cat4 36.48
cat5 45.59
np.round 返回
np.round(df1, 1)
total
cat1 25.2
cat2 3.6
cat3 76.6
cat4 36.5
cat5 45.6
appymap 返回
df1.applymap(lambda x: round(x,1))
total
cat1 25.2
cat2 3.5
cat3 76.5
cat4 36.5
cat5 45.6
如您所见,np.round 向上取整中值,而 applymap 向下取整。怎么回事?
【问题讨论】:
-
两种方法的值相同,python
3.4.3,numpy1.10.1 -
以这种方式舍入浮点数实际上是没有用的,尽管这样做的功能太容易了。您在这里四舍五入的最终目的是什么?
-
@AntonProtopopov
round行为在 python 3: docs.python.org/3/library/functions.html#round 和 2: docs.python.org/2/library/functions.html#round 之间是不同的 -
@EdChum 是的,谢谢。但是OP没有提到任何python版本..
标签: numpy pandas decimal rounding