如何在 Jupyter 笔记本中设置 pandas.DataFrame.loc 的输出精度?

能否在 Jupyter 笔记本中提高 pandas.DataFrame.loc 函数的表格输出的精度?

我希望看到下表中 $f'_{cds}$ 的输出以 16 位十进制数字精度编写:

http://img4.mukewang.com/610903e200013da106830217.jpg

pd.set_option('precision', 16)

为熊猫设置输出精度,但我希望能够仅为 $f'_{cds}$ 列设置精度。


这是我正在使用的示例数据集:


h,$f' {ex}$,$f' {fwd}$,$E_{fwd}$,$E_{fwdn}$,$f' {cds}$,$E{CDS},$ {E_ CDSN} $ 0.5 -0.2955202066613395,-0.5172595595568812,0.2217393528955416,0.750335672137813,-0.2833598684940762,0.01216033816726331,0.04114892279159381 0.25,-0.2955202066613395,-0.4112478682644012,0.1157276616030616,0.3916065940481806,-0.2924514766709212,0.003068729990418351,0.01038416298190755 0.125, - 0.2955202066613395,-0.3543820493725782,0.05886184271123868,0.1991804329600149,-0.294751223803599,0.0007689828577405744,0.002602132918179141 0.0625,-0.2955202066613395,-0.325172396632464,0.02965218997112445,0.1003389592411367,-0.2953278482673038,0.0001923583940356965,0.0006509145219167214 0.03125,-0.2955202066613395,-0.3103980279268619,0.01487782126552234,0.05034451428416885,-0.2954721100178972,4.809664344235243e -05,0.0001627524695712948 0.015625,-0.2955202066613395,-0.3029715965360111,0.007451389874671585281048881048104042955081820601322,1.202460120736104e-05,4.068960746613514e-05 0.0078125,-0.2955202066613395,-0.2992489646633629,0.00372875800202338,0.01261760758815544,-0.2955172004835163,3.006177823283718e-06,1.017249499533797e-05 0.00390625,-0.2955202066613395,-0.297385344322862,0.001865137661522465,0.00631137099758419,-0.295519455115155, 7.515461845630789e-07,2.543129598661713e-06

对于示例数据集,Mohit 的答案如下:

sqrerr.loc[:, ("h","$E_{cds}$","$f'_{ex}$", "$f'_{cds}$")].style.format({"$f'_{cds}$" : '{:.16f}'})


慕勒3428872
浏览 321回答 3
3回答

白衣染霜花

导入 pandas 库后,将这些行添加到您的代码中pd.set_option('precision', 0) pd.set_option('display.float_format', lambda x: '%.0f' % x)

神不在的星期二

df.style.format 仅为当前输出设置精度。如果您再次调用“df”,它将与您导入的一样。根据 Malik Asad 的回答,我在 lambda 函数中添加了 if-else 条件,以便您可以删除尾随零 (.0) 并在数据框中为每个具有数值的单元格设置字面上的“个人”精度:        pd.set_option('display.float_format', lambda x: '%.0f' % x                        if (x == x and x*10 % 10 == 0)                        else ('%.1f' % x if (x == x and x*100 % 10 == 0)                                             else '%.2f' % x))当然,您可以通过添加更多选项来改进它。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python