我是pandas python的新手,我在尝试对列中的所有值进行舍入时遇到了困难.例如,
Example
88.9
88.1
90.2
45.1
Run Code Online (Sandbox Code Playgroud)
我尝试使用下面的当前代码,但它给了我:
AttributeError:'str'对象没有属性'rint'
df.Example = df.Example.round()
Run Code Online (Sandbox Code Playgroud)
你可以使用numpy.ceil:
In [80]: import numpy as np
In [81]: np.ceil(df.Example)
Out[81]:
0 89.0
1 89.0
2 91.0
3 46.0
Name: Example, dtype: float64
Run Code Online (Sandbox Code Playgroud)
根据您的喜好,您还可以更改类型:
In [82]: np.ceil(df.Example).astype(int)
Out[82]:
0 89
1 89
2 91
3 46
Name: Example, dtype: int64
Run Code Online (Sandbox Code Playgroud)
编辑
您的错误消息表明您正在尝试四舍五入(不一定是向上),但遇到类型问题.你可以像这样解决它:
In [84]: df.Example.astype(float).round()
Out[84]:
0 89.0
1 88.0
2 90.0
3 45.0
Name: Example, dtype: float64
Run Code Online (Sandbox Code Playgroud)
在这里,您也可以在结尾处转换为整数类型:
In [85]: df.Example.astype(float).round().astype(int)
Out[85]:
0 89
1 88
2 90
3 45
Name: Example, dtype: int64
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7021 次 |
| 最近记录: |