Home > Enterprise >  Pandas styler does not maintain rounding
Pandas styler does not maintain rounding

Time:12-13

I would like to round off Pandas DataFrame and then add a title to it, but Pandas style.set_caption command ruins the rounding.

example:

pd.DataFrame(np.array([[0.555,0.444],[0.333,0.222]])).round(decimals = 2).style.set_caption("hello")

result:

hello
    0   1
0   0.560000    0.440000
1   0.330000    0.220000

How can I add a title while maintaing rouding?

CodePudding user response:

The answer is enter image description here

CodePudding user response:

It certainly has maintained the rounding. It is just not printing with 2 decimals. Those are two entirely different concepts, and it is important to understand that if you're working with floating point data. You can use pd.options.display.float_format to change the way pandas displays floats.

Also note that, in general, it is better to keep all the decimal places in your computations, and just do the rounding when you display. If your value is 0.555, then you should compute with 0.555, and have whatever is doing the printing show it as 0.56. Humans care about limiting the decimals places. Computers don't.

  • Related