How to easily format output (e.g. rounded numbers) in a Jupyter notebook?

Suppose I have a dataframe and I want to show it in Jupyter notebook.
That’s simple: the screenshot below shows two ways to do so.


(Where personally I like the layout of the first one, without print, better.)

However, instead I want to show rounded numbers for sat_score? (1157, 1181, etc.)

This should be simple I guess - but I was not sure actually how to do this best :-(.
I could think of multiple ways:

  1. Does Jupyter notebook itself contains settings to format the output?
  2. Or should I use particular Python code to format? If so: what code?
  3. Update the data itself, round it in the dataframe. Dismissed. I want to keep my accurate data.
  4. Add an additional column to my dataframe with rounded values and show that instead. Also dismissed, there should be another way.

How to do this (best)…?

1 Like

Hi, jasperquak,
use pandas format method, create own dictionary with required format for needed columns
but don’t use the command print - it clear format
instead print for notebook use display()
for example see my notebook Mobile App for Lottery Addiction in the Guided Project

from IPython.display import display
display({“combinations”:"{:,.2f}", “chance_win_%”: “{:.4g}”}))


Hi @jasperquak. As you can imagine, there are several ways to accomplish your task.

  1. You can use a magic command so that all floats throughout the notebook are formatted the same:
%precision %.2f
  1. You can use this code to format individual cells to your liking:
pandas.options.display.float_format = '{:.2f}'.format

Hope this helps!


Thank you, @vadim.maklakov and @mathmike314 , very helpful!

For this occasion I used:

pandas.options.display.float_format = '{:.2f}'.format

as it was straightforward and did the trick. (I applied it in this guided project.)

This would not work if I were to be more precise and wanted to set a specific layout for every individual column of the dataframe. I am not sure yet how to do that most efficiently. Probably if I study the materials you referred me to I can figure that out though…