Pandas 熊猫:停止熊猫自动将0.40显示为0.4
如何阻止pandas自动将数据帧值中的0.40转换为0.4 我相信解决方案与“显示”选项和格式有关?但是我找不到任何信息 比如:Pandas 熊猫:停止熊猫自动将0.40显示为0.4,pandas,dataframe,formatting,display,Pandas,Dataframe,Formatting,Display,如何阻止pandas自动将数据帧值中的0.40转换为0.4 我相信解决方案与“显示”选项和格式有关?但是我找不到任何信息 比如: display.float_format(#.##) 您需要将格式字符串对象作为参数传递给显示。float\u format: In [167]: pd.set_option('display.float_format', '{0:.2f}'.format) df = pd.DataFrame(np.random.randn(5,3)) df Out[167]:
display.float_format(#.##)
您需要将格式字符串对象作为参数传递给
显示。float\u 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
使用列表理解更改特定系列中的所有值。将
.2
更改为所需的精度级别
a = ["{0:.2f}".format(val) for val in df.column_name]
df.loc[:,'column_name'] = a
这正是我想要的。打印df可以正确显示值,但是当我将df写入excel时,它仍然显示为0.4?知道吗?这只是一个显示格式问题吗?例如,当您移动到特定单元格时,它会显示什么值?我不确定这是否会影响数据的导出方式,我不熟悉python excel模块。说
to\u excel
也支持float\u格式
arg:所以你可以试试看,试了没有效果,现在没那么重要,所以竖起大拇指,再次感谢Ed!