用 pandas 舍入数字
我有一个 pandas 数据框,其中有一列包含数字: [4.534000e-01, 6.580000e-01, 1.349300e+00, 2.069180e+01, 3.498000e-01,...]
我想将此列四舍五入到小数点后 3 位,例如我使用 round(col) 函数;但是,我注意到 panda 给了我以下内容: [0.453, 0.658, 1.349, 20.692, 0.35,...]
其中最后一个元素的小数点后没有三位数字。
我希望将所有数字四舍五入为相同数量的数字,例如:[0.453, 0.658, 1.349, 20.692, 0.350,...]
。
如何在 pandas 中做到这一点?
I have a pandas dataframe with a column that contains the numbers:[4.534000e-01, 6.580000e-01, 1.349300e+00, 2.069180e+01, 3.498000e-01,...]
I want to round this column up to 3 decimal places, for which I use the round(col) function; however, I have noticed that panda gives me the following:[0.453, 0.658, 1.349, 20.692, 0.35,...]
where the last element doesn't have three digits after the decimal.
I would like to have all the numbers rounded with the same amount of digits, for example, like: [0.453, 0.658, 1.349, 20.692, 0.350,...]
.
How can be done this within pandas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您可以使用pandas.DataFrame.round来指定精度。
https://pandas.pydata.org/docs/reference/ api/pandas.DataFrame.round.html
You can use
pandas.DataFrame.round
to specify a precision.https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.round.html