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

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.

Leave a Comment