首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将数字四舍五入为小数后的四位数

将数字四舍五入为小数后的四位数
EN

Stack Overflow用户
提问于 2019-03-25 18:19:20
回答 1查看 27关注 0票数 1

我正在标准化我的数据,并希望最终存储一个四舍五入的小数后4位数字。

下面的语句给了我一个标准化的结果,但是我如何将每个数字舍入到小数点后的4位数字。

代码语言:javascript
运行
复制
df_new = df.loc[:,"pos_score":"neg_sub_score"].div(100, axis=0)

原创:18.685714285714287

归一化后:.1868 (需要)

归一化后:.18685714285714287 (当前)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-25 18:25:15

如果需要下限值乘以100,请转换为整数并除以10000

代码语言:javascript
运行
复制
df = pd.DataFrame({'pos_score':[18.68577,18.685714],
                   'neg_sub_score':[10.23, 12.536]})
print (df)
   pos_score  neg_sub_score
0  18.685770         10.230
1  18.685714         12.536

df_new1 = df.loc[:,"pos_score":"neg_sub_score"].mul(100).astype(int).div(10000)
print (df_new1)
   pos_score  neg_sub_score
0     0.1868         0.1023
1     0.1868         0.1253

但如果需要舍入值,则添加DataFrame.round,但输出不同:

代码语言:javascript
运行
复制
df_new2 = df.loc[:,"pos_score":"neg_sub_score"].div(100).round(4)
print (df_new2)
   pos_score  neg_sub_score
0     0.1869         0.1023
1     0.1869         0.1254
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55335563

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档