更改数据框中的单个浮点数

问题描述 投票:0回答:1

我的作业快完成了。然而,我只需对最终数据框进行一个小的调整。 对于“CL_Total”列,第一行号 157.580 已更改为两位小数,而不是三位。我只需更改这个号码即可。 有人可以帮助我吗?

enter image description here

数据框应如下所示: enter image description here

我现在的代码是这样的,但是数字不会改变两位小数: df_final['CL_total'].iloc[0] == df_final['CL_total'].iloc[0].round(2)

打印(df_final.iloc[:6])

python pandas dataframe decimal rounding
1个回答
0
投票

CL_total 中的数字是整数还是字符串(在这种情况下字符串不能四舍五入) 请使用

df_final.dtypes
进行检查。

df_final["CL_total"].astype(int) # conver the strings of CL_total to ints

df_final['CL_total'].iloc[0] == df_final['CL_total'].iloc[0].round(2)

astype()
方法文档在这里

© www.soinside.com 2019 - 2024. All rights reserved.