圆 pandas 柱,精度高,但无尾随0 [英] round pandas column with precision but no trailing 0
问题描述
不是重复的,因为我在问熊猫round()
.
Not duplicate because I'm asking about pandas round()
.
我有一个dataframe
,其中有些列带有数字.我跑
I have a dataframe
with some columns with numbers. I run
df = df.round(decimals=6)
这成功地截断了小数点,而不是正确地写出15.36785699998
的内容:15.367857
,但是我仍然得到带有尾随零的1.0
或16754.0
.
That successfully truncated the long decimals instead of 15.36785699998
correctly writing: 15.367857
, but I still get 1.0
or 16754.0
with a trailing zero.
运行熊猫df.round()
后,如何消除所有列中的尾随零?
How do I get rid of the trailing zeros in all the columns, once I ran pandas df.round()
?
我想将数据帧另存为csv,并且需要数据来显示我希望的方式.
I want to save the dataframe as a csv, and need the data to show the way I wish.
推荐答案
df = df.round(decimals=6).astype(object)
转换为object
将允许混合表示.但是,请记住,从性能角度来看,这不是很有用.
Converting to object
will allow mixed representations. But, keep in mind that this is not very useful from a performance standpoint.
df
A B
0 0.149724 -0.770352
1 0.606370 -1.194557
2 10.000000 10.000000
3 10.000000 10.000000
4 0.843729 -1.571638
5 -0.427478 -2.028506
6 -0.583209 1.114279
7 -0.437896 0.929367
8 -1.025460 1.156107
9 0.535074 1.085753
df.round(6).astype(object)
A B
0 0.149724 -0.770352
1 0.60637 -1.19456
2 10 10
3 10 10
4 0.843729 -1.57164
5 -0.427478 -2.02851
6 -0.583209 1.11428
7 -0.437896 0.929367
8 -1.02546 1.15611
9 0.535074 1.08575
这篇关于圆 pandas 柱,精度高,但无尾随0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!