Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

python - 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?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...