0

Imagine I have this list:

df = pandas.DataFrame([1, 2.321, 13.3444, 7.5, 5])

How can I make the output table show whole numbers without decimal places and others with, say 2, decimal places? Something like this:

1 2.30 13.34 7.50 5

instead of this:

1.00 2.30 13.34 7.50 5.00
Shahriar
  • 1,855
  • 2
  • 21
  • 45
  • You should not try to control float format, **unless** you're willing to convert to string, or settle for a fixed format (see second/third duplicate) – mozway Apr 05 '23 at 08:55

1 Answers1

2

Assuming that you dont want to change the numbers themselves but only the way they are presented, this is the way to go:

pd.set_option('display.float_format', '{:.2f}'.format)

In case you want to change the underlying numbers, there is a round function that can be applied to DataFrames or columns, such as

df.round(2)  # two decimals
Klops
  • 951
  • 6
  • 18
  • I ended up with `pd.set_option('display.float_format', lambda x: f'%.{0 if x.is_integer() else 1}f' % x)` – Shahriar Apr 05 '23 at 09:26