我有一个pandas数据框,其中一些列有数值,而其他列没有,如下所示:
City a b c
Detroit 129 0.54 2,118.00
East 188 0.79 4,624.4712
Houston 154 0.65 3,492.1422
Los Angeles 266 1.00 7,426.00
Miami 26 0.11 792.18
MidWest 56 0.24 772.7813
我想将这些数值四舍五入到小数点后2位,我使用的是:
df = df.replace(np.nan, '', regex=True)
在此之后,df变为:
City a b c
Detroit 129.0 0.54 2,118.0
East 188.0 0.79 4,624.47
Houston 154.0 0.65 3,492.14
Los Angeles 266.0 1.0 7,426.0
Miami 26.0 0.11 792.18
MidWest 56.0 0.24 772.78
它工作得很好,但它也将适当的整数转换为小数,即,像100这样的值被舍入为100.0。我希望数据帧如下所示:
City a b c
Detroit 129 0.54 2,118
East 188 0.79 4,624.47
Houston 154 0.65 3,492.14
Los Angeles 266 1 7,426
Miami 26 0.11 792.18
MidWest 56 0.24 772.28
我希望将这些值本身保留为正确的整数,同时在所有数字列中将其他值舍入到小数点后2位。我该怎么做
使用^{} :
相关问题 更多 >
编程相关推荐