Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 748
Ryan Reid Avatar asked Dec 02 '25 09:12

Ryan Reid


1 Answers

If you want to only modify the format of your values without doing any operation in pandas, you should just execute the following instruction:

pd.options.display.float_format = "{:,.2f}".format

This forces it not to use scientific notation (exponential notation) and always displays 2 places after the decimal point. It also adds commas.

You should be able to get more info here:

https://pandas.pydata.org/docs/user_guide/options.html#number-formatting

Examples:

0.0012             0.00
0.0123             0.01
1.2345             1.23
12.345             12.35
100                100.00
1234567890.123456  1,234,567,890.12
 
like image 124
Zombraz Avatar answered Dec 07 '25 11:12

Zombraz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!