简体   繁体   English

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

[英]How to set the precision for the output of the pandas.DataFrame.loc in a Jupyter notebook?

Can the precision of the tabular output of the pandas.DataFrame.loc function be increased inside a Jupyter notebook?能否在 Jupyter 笔记本中提高 pandas.DataFrame.loc 函数的表格输出的精度?

I would like to see the output of the $f'_{cds}$ in the table below written with a 16 decimal digit precision:我想看到下表中 $f'_{cds}$ 的输出以 16 位十进制数字精度编写:

Jupyter notebook 中 pandas.DataFrame.loc 表格输出的精度k

pd.set_option('precision', 16)

Sets the output precison for pandas, but I would like to be able to set the precision just for the $f'_{cds}$ column.为熊猫设置输出精度,但我希望能够仅为 $f'_{cds}$ 列设置精度。

This is the example dataset I am using:这是我正在使用的示例数据集:

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.007451389874671588,0.02521448519156843,-0.2955081820601322,1.202460120736104e-05,4.068960746613514e-05 0.0078125,-0.2955202066613395,-0.2992 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.007451389874671588,0.02521448519156843,-0.2955081820601322,1.202460120736104e-05,4.068960746613514e-05 0.0078125,-0.2955202066613395,-0.2992 489646633629,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 0.001953125,-0.2955202066613395,-0.2964529642682692,0.000932757606929624,0.003156324291551901,-0.2955200187747096,1.878866299764859e-07,6.357826833540365e-07 0.0009765625,-0.2955202066613395,-0.2959866325476241,0.0004664258862845938,0.001578321467604782,-0.2955201596896586,4.697168093370507e-08,1.589457501548573e-07 489646633629,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 0.001953125,-0.2955202066613395,-0.2964529642682692, 0.000932757606929624,0.003156324291551901,-0.2955200187747096,1.878866299764859e-07,6.357826833540365e-07 0.0009765625,-0.2955202066613395,-0.2959866325476241,0.0004664258862845938,0.001578321467604782,-0.2955201596896586,4.697168093370507e-08,1.589457501548573e-07

For the example dataset, the answer from Mohit works like this:对于示例数据集,Mohit 的答案如下:

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

Use df.style :使用df.style

df.style.format('{:.16f}')

Let me know if it works让我知道它是否有效

For column wise operation:对于列操作:

df.style.format({'A': '{:.16f}', 'D': '{:.5f}'})

导入 pandas 库后,将这些行添加到您的代码中

pd.set_option('precision', 0) pd.set_option('display.float_format', lambda x: '%.0f' % x)

df.style.format sets precision only for current output. df.style.format 仅为当前输出设置精度。 If you call "df" once again, it'll be the same like you've imported.如果您再次调用“df”,它将与您导入的一样。

Based on the answer of Malik Asad, I've added if-else conditions in lambda-function so that you could remove trailing-zeros (.0) and set literally "personal" precision for each cell with numeric-value in dataframe:根据 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))

Example in Python Python 中的示例

Of course, you can improve it by adding more options.当然,您可以通过添加更多选项来改进它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM