How to round values only for display in pandas while retaining original ones in the dataframe?
pandas, python, rounding
Solution
You can temporarily change the display option:
with pd.option_context('display.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('display.precision', 3)
A simple `print(df.head().round(3))` would also work in this case. They will not change the DataFrame in place.
Problem
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.