pandas:停止 pandas 自动将 0.40 显示为 0.4
pandas: stop pandas from auto displaying 0.40 as 0.4
如何阻止 pandas 自动将数据帧值中的 0.40 转换为 0.4?
我认为解决方案与 'display' 选项和格式有关?但我找不到任何信息。
类似于:
display.float_format(#.##)
您需要将格式字符串对象作为参数传递给 set_option
for 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
使用列表理解来更改特定系列中的所有值。将 .2
更改为您想要的任何精度级别。
a = ["{0:.2f}".format(val) for val in df.column_name]
df.loc[:,'column_name'] = a
如何阻止 pandas 自动将数据帧值中的 0.40 转换为 0.4?
我认为解决方案与 'display' 选项和格式有关?但我找不到任何信息。
类似于:
display.float_format(#.##)
您需要将格式字符串对象作为参数传递给 set_option
for 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
使用列表理解来更改特定系列中的所有值。将 .2
更改为您想要的任何精度级别。
a = ["{0:.2f}".format(val) for val in df.column_name]
df.loc[:,'column_name'] = a