pandas 系列向下舍入中位数和平均值
pandas series round down median and mean values
我有一个series
,
s = pd.Series([1.115,2.337,3.225])
s.mean()
2.2256666666666667
我想知道如何将mean()
和median()
值四舍五入,这样如果小数点后第三位的数字是5,它应该向下舍入,所以结果应该是2.22
而不是 2.2256666666666667
.
numpy.around
will round to Nearest Even:
>>> s = pd.Series([1.115,2.337,3.225])
>>> s.mean()
2.2256666666666667
>>> np.around(s.mean(), decimals=4)
2.2257
>>> np.around(s.mean(), decimals=3)
2.226
>>> np.around(s.mean(), decimals=2)
2.23
...但是:
if the number at the 3rd decimal place is 5, it should round down [...] should be 2.22 instead of 2.2256666666666667.
这不仅仅是四舍五入;只有当你截断到小数点后 3 位然后四舍五入到两位时,你才会得到 2.22:
>>> (np.trunc([s.mean() * 1000.]) / 1000.)[0]
2.225
>>> (np.around(np.trunc([s.mean() * 1000.]) / 1000., decimals=2))[0]
2.22
我有一个series
,
s = pd.Series([1.115,2.337,3.225])
s.mean()
2.2256666666666667
我想知道如何将mean()
和median()
值四舍五入,这样如果小数点后第三位的数字是5,它应该向下舍入,所以结果应该是2.22
而不是 2.2256666666666667
.
numpy.around
will round to Nearest Even:
>>> s = pd.Series([1.115,2.337,3.225])
>>> s.mean()
2.2256666666666667
>>> np.around(s.mean(), decimals=4)
2.2257
>>> np.around(s.mean(), decimals=3)
2.226
>>> np.around(s.mean(), decimals=2)
2.23
...但是:
if the number at the 3rd decimal place is 5, it should round down [...] should be 2.22 instead of 2.2256666666666667.
这不仅仅是四舍五入;只有当你截断到小数点后 3 位然后四舍五入到两位时,你才会得到 2.22:
>>> (np.trunc([s.mean() * 1000.]) / 1000.)[0]
2.225
>>> (np.around(np.trunc([s.mean() * 1000.]) / 1000., decimals=2))[0]
2.22