How do you display values in a pandas dataframe column with 2 decimal places?

Viewed 8270

What I am looking to do is make it so that regardless of the value, it displays 2 decimal places.

What I have tried thus far:

DF['price'] = DF['price'].apply(lambda x: round(x, 2))

However, the problem is that I wish to display everything in 2 decimal places, but values like 0.5 are staying at 1 decimal place since they don't need to be rounded.

Is there a function I can apply that gives the following type of output:

Current        After Changes
0              0.00
0.5            0.50
1.01           1.01
1.133333       1.13

Ideally, these values will be rounded but I am open to truncating if that is all that works.

4 Answers

I think you want something like this

DF['price'] = DF['price'].apply(lambda x: float("{:.2f}".format(x)))

This applies the change just to that column

You have to set the precision for pandas display. Put this on top of your script after importing pandas:

import pandas as pd
pd.set_option('precision', 2)

Try:

import pandas as pd
pd.set_option('display.precision', 2)
Related