Pandas:使用 np.round 和 applymap 舍入数据框中的中间值

Pandas: rounding halfway values in dataframe using np.round and applymap

我想了解为什么在同一个 DF

上使用 1) np.round 和 2) applymap 时得到不同的值

我的 df

 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 returns

np.round(df1, 1)
      total
cat1   25.2
cat2    3.6
cat3   76.6
cat4   36.5
cat5   45.6

appymap returns

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 2 中记录的行为:round and np.around 在 python 3 中得到相同的结果:

In [63]:
np.round(df1['total'], 1)

Out[63]:
cat1    25.2
cat2     3.6
cat3    76.6
cat4    36.5
cat5    45.6
Name: total, dtype: float64

In [69]:
df1.applymap(lambda x: round(x,1))

Out[69]:
      total
cat1   25.2
cat2    3.6
cat3   76.6
cat4   36.5
cat5   45.6