我有包含数据的 CSV 文件:
Number
1.1
2.2
4.1
5.4
9.176
14.54345774
16.25664
Run Code Online (Sandbox Code Playgroud)
如果我打印并显示,pandas我会得到:
df = pd.read_csv('data.csv')
print(df)
Number
0 1.100000
1 2.200000
2 4.100000
3 5.400000
4 9.176000
5 14.543458
6 16.256640
Run Code Online (Sandbox Code Playgroud)
但如果我切14.54345774到14.543输出改变:
Number
0 1.10000
1 2.20000
2 4.10000
3 5.40000
4 9.17600
5 14.54300
6 16.25664
Run Code Online (Sandbox Code Playgroud)
第一种情况小数点后位数pandas为 6,第二种情况为 5。
为什么格式要改变?我应该更改哪些pandas参数以使这些情况相同?我希望小数点后的位数保持不变,并且如果可能的话,小数点后的位数四舍五入到小数点后的最大位数。
更新:
IMO,这一刻出现在数据初始化时,所以round如果我想使用 6 位数字,就不会得到理想的结果。只能减少(6->5 位),但不能增加(5->6)。
在这种情况下,您可以将pd.set_option十进制数字显示精度设置为例如 5:
pd.set_option("display.precision", 5)
Run Code Online (Sandbox Code Playgroud)
或使用:
pd.options.display.float_format = '{:.5f}'.format
Run Code Online (Sandbox Code Playgroud)
结果:
print(df) # with original value of 14.54345774
Number
0 1.10000
1 2.20000
2 4.10000
3 5.40000
4 9.17600
5 14.54346
6 16.25664
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15201 次 |
| 最近记录: |