Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to round values only for display in pandas while retaining original ones in the dataframe?

I wish to only round values in the DataFrame for display purposes, when I use head() or tail() but I want the DataFrame to retain the original values.

I tried using the round method but it changes the values in the original DataFrame. I don't wish to create a separate copy each time for this purpose.

Is there any other way than creating a separate copy?

I'm having trouble glancing at values because some columns have e^10 notations. I'd just like to have a look at two to three decimal places maximum and not keep glancing at exponent values.

like image 829
xennom Avatar asked Jul 07 '17 06:07

xennom


1 Answers

You can temporarily change the display option:

with pd.option_context('precision', 3):
    print(df.head())

       0      1      2      3      4
0 -0.462 -0.698 -2.030  0.766 -1.670
1  0.925  0.603 -1.062  1.026 -0.096
2  0.589  0.819 -1.040 -0.162  2.467
3 -1.169  0.637 -0.435  0.584  1.232
4 -0.704 -0.623  1.226  0.507  0.507

Or change it permanently:

pd.set_option('precision', 3)

A simple print(df.head().round(3)) would also work in this case. They will not change the DataFrame in place.

like image 64
ayhan Avatar answered Oct 10 '22 16:10

ayhan