我必须将数据框中的一列数字格式化为 pandas 四舍五入为两位但保留三位十进制数字,其中最后一位为 0 并且该列不必是字符串。导出到 Excel 后,必须表示该列的数字。
d = {'col1': [0.567, 2.54765476], 'col2': [3, 4]}
df = pd.DataFrame(data=d)
col1 col2
0 0.567 3
1 2.54765476 4
预期:
col1 col2
0 0.560 3
1 2.540 4
重要的是类型保持数字
最佳答案
您可能需要floor
div 然后format
df.col1=(np.floor(df.col1*100)/100).map('{:,.3f}'.format)
df
Out[355]:
col1 col2
0 0.560 3
1 2.540 4
关于python - 使用指定的十进制位数格式化 pandas 数据框的数字列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54770993/