Python 对序列进行舍入时出错
我有一系列的花车。它是数据帧sum()操作的结果。 我需要将其所有元素舍入为整数,但我得到一个错误:Python 对序列进行舍入时出错,python,pandas,rounding,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 c
[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'
现在,如果我尝试舍入,我会得到一个错误:
[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'
[in]:
A.第(轮)
[out]:
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在()
3.
4印刷品(A)
---->5 A.第(轮)
//anaconda/lib/python3.5/site-packages/pandas/core/series.py四舍五入(self、小数、*args、**kwargs)
1303 """
1304内华达州验证轮(ARG、kwargs)
->1305结果=来自对象(自身)的值。舍入(小数)
1306结果=self.\u构造函数(结果,索引=self.index)。\uuuuuu最终确定(self)
1307
AttributeError:“float”对象没有属性“rint”
谁能告诉我这是为什么?我能修好它吗?
我想问题的根源是因为这个系列是“对象”类型的。
但这是什么?它只包含浮点数!!!它是数据帧摘要的结果
提前感谢您按照Mark Dickinson的建议,使用
.astype(float)
将该系列转换为float类型。
在您的情况下,应该使用A.astype(float).round()
我在数据框中遇到了同样的错误,将列舍入为一个浮点值为我解决了这个问题。多亏了马克·迪金森。你可以尝试a.astype(float)
。我已经尝试过了,但也不起作用。…@user3623123:a.astype(float).round()
应该能起作用。你能展示一下你尝试它时出现了什么错误吗?