微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python – 舍入一列

我是pandas python的新手,我在尝试整理列中的所有值时遇到了困难.例如,

Example
88.9
88.1
90.2
45.1

我尝试使用下面的当前代码,但它给了我:

AttributeError: ‘str’ object has no attribute ‘rint’

 df.Example = df.Example.round()

解决方法:

你可以使用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

根据您的喜好,您还可以更改类型:

In [82]: np.ceil(df.Example).astype(int)
Out[82]: 
0    89
1    89
2    91
3    46
Name: Example, dtype: int64

编辑

您的错误消息表明您正在尝试四舍五入(不一定是向上),但遇到类型问题.你可以像这样解决它:

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

在这里,您也可以在结尾处转换为整数类型:

In [85]: df.Example.astype(float).round().astype(int)
Out[85]: 
0    89
1    88
2    90
3    45
Name: Example, dtype: int64

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐