Mau*_*ile 5 python rounding pandas
我有一系列的花车。它是数据帧 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
Run Code Online (Sandbox Code Playgroud)
现在,如果我尝试四舍五入,则会收到错误:
[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'
Run Code Online (Sandbox Code Playgroud)
谁能告诉我为什么会这样,我能解决它吗?我猜问题的根源是因为该系列是“对象”类型。但这是什么?它只包含浮点数!它是数据帧汇总的结果
预先感谢您的帮助
.astype(float)按照 Mark Dickinson 的建议,将系列转换为 float 类型。在你的情况下你应该使用A.astype(float).round()
我在数据框中舍入一列时遇到了同样的错误,转换为浮点数为我解决了这个问题。感谢马克·狄金森。
| 归档时间: |
|
| 查看次数: |
9612 次 |
| 最近记录: |