热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

python–舍入一列

我是pandaspython的新手,我在尝试整理列中的所有值时遇到了困难.例如,Example88.988.190.245.1我尝试使用下面的当前代码,但它给了我:Attribut

我是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


推荐阅读
author-avatar
北关仔仔_378
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有