问题
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?
Edit : 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.
Edit 2 : Solved. Thanks for the quick response.
回答1:
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.
来源:https://stackoverflow.com/questions/44963769/how-to-round-values-only-for-display-in-pandas-while-retaining-original-ones-in