从一列字符串中提取浮点数并四舍五入到小数点后两位 [英] Extract floats from a column of strings and round to 2 decimal places

查看:55
本文介绍了从一列字符串中提取浮点数并四舍五入到小数点后两位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一列中包含值的数据框

If i have a data frame with values in a column

4.5678
5
7.987.998

我只想提取小数点后2个值的数据

I want to extract data for only 2 values after the decimal

4.56
5
7.98

数据存储为字符串.任何帮助将不胜感激.谢谢 !

The data is stored as a string. Any help will be appreciated. Thank you !

推荐答案

str = "7.987.998"
ind = str.find('.')
if ind > 0:
  res = str[:ind+3]

这篇关于从一列字符串中提取浮点数并四舍五入到小数点后两位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆