四舍五入系列时出错

时间:2016-12-07 11:10:07

标签: python pandas rounding

我有一系列花车。它是数据帧sum()操作的结果。 我需要将其所有元素舍入为整数但我收到错误:

[in]:
A= mins.sum().iloc[1:]/60 
# this line works fine. The .iloc is to get rid of a text column.

[in]:
print(A)

[out]:
Min bad                     249.5
Min pr-ul                   967.57
intra com diff              178.05
Intra com diff 60           184.27
dtype: object

现在,如果我尝试回合,我会收到错误:

[in]:
A.round()

[out]:
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-528-685b5302b717> in <module>()
  3 
  4 print(A)
  ----> 5 A.round()

 //anaconda/lib/python3.5/site-packages/pandas/core/series.py in round(self,decimals, *args, **kwargs)
   1303         """
   1304         nv.validate_round(args, kwargs)
   -> 1305         result = _values_from_object(self).round(decimals)
   1306         result = self._constructor(result, index=self.index).__finalize__(self)
   1307 

   AttributeError: 'float' object has no attribute 'rint'

任何人都可以告诉我为什么会这样,我可以解决它吗? 我想这个问题的根源是因为Series是“对象”类型。 但这是什么?它只包含浮点数!!!它是数据框摘要的结果

提前感谢您的帮助

1 个答案:

答案 0 :(得分:0)

按照马克·迪金森(Mark Dickinson)的建议,使用.astype(float)对系列进行键入以键入float。 在您的情况下,您应该使用A.astype(float).round()

我在数据框中四舍五入时遇到了同样的错误,强制转换为浮点数为我解决了。感谢马克·狄金森。