Formatting Numbers

Round at the end, and show the precision you actually have.

Round for the reader, not for the maths

Rounding partway through loses precision that never comes back. Keep the working exact and round once, at the point of display.

import pandas as pd

s = pd.Series([1.0, 2.0, 2.0])
print(round(float(s.mean()), 2))

Precision is a claim

Reporting a mean to six decimal places from thirty measurements claims an accuracy you do not have. One or two places is usually the honest amount.

import pandas as pd

s = pd.Series([90.0, 80.0, 71.0])
print(round(float(s.mean()), 1), round(float(s.std()), 1))

Exercise

Try It Yourself

Write summary_line(s) returning "mean 82.0 (min 70.0, max 95.0)" for a Series, every number to one decimal place.

Press Run to see output

Check Your Understanding