如何舍入熊猫Float64Index?

时间:2020-03-03 11:04:37

标签: python pandas

我在熊猫中有一个Float64Index,并希望将其舍入到3个小数点。对于数据帧,没有像round()这样的等效项。如何将Float64Index舍入到特定的浮点精度?

1 个答案:

答案 0 :(得分:2)

使用numpy.round

df = pd.DataFrame({
        'A':list('abcdef'),

}, index=[5.2563,3.25633,6.4538,9.236,2.236,4.2337])

df.index = np.round(df.index, 3)
print (df)
       A
5.256  a
3.256  b
6.454  c
9.236  d
2.236  e
4.234  f

或将索引转换为Series并调用Series.round

df.index = df.index.to_series().round(3)