我正在标准化我的数据,并希望最终存储一个四舍五入的小数后4位数字。
下面的语句给了我一个标准化的结果,但是我如何将每个数字舍入到小数点后的4位数字。
df_new = df.loc[:,"pos_score":"neg_sub_score"].div(100, axis=0)原创:18.685714285714287
归一化后:.1868 (需要)
归一化后:.18685714285714287 (当前)
发布于 2019-03-25 18:25:15
如果需要下限值乘以100,请转换为整数并除以10000
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,但输出不同:
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.1254https://stackoverflow.com/questions/55335563
复制相似问题