如何阻止 pandas 自动将数据帧值中的 0.40 转换为 0.4?
我相信解决方案与“显示”选项和格式有关?但我找不到任何信息。
类似于:
display.float_format(#.##)
最佳答案
您需要将格式字符串对象作为参数传递给 set_option
对于 display.float_format
:
In [167]:
pd.set_option('display.float_format', '{0:.2f}'.format)
df = pd.DataFrame(np.random.randn(5,3))
df
Out[167]:
0 1 2
0 0.84 -0.91 -1.44
1 1.82 0.38 -0.47
2 -0.07 2.09 1.81
3 0.09 -2.05 -0.92
4 0.26 -1.94 -1.09
底层数据不受影响:
In [168]:
df.iloc[0][0]
Out[168]:
0.84179409715165521
关于pandas:阻止 pandas 自动将 0.40 显示为 0.4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39492607/