我有一系列花车。它是数据帧 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'
谁能告诉我这是为什么,我能解决吗? 我想问题的根源是因为系列是“对象”类型。 但这是什么?它只包含 float !!!它是数据框汇总的结果
提前感谢您的帮助
请您参考如下方法:
按照 Mark Dickinson 的建议,使用 .astype(float) 将系列转换为 float 类型。 在您的情况下,您应该使用 A.astype(float).round()
我在数据框中舍入一列时遇到了同样的错误,转换为 float 为我解决了这个问题。感谢马克·狄金森。
