4

Not duplicate because I'm asking about pandas round().

I have a dataframe with some columns with numbers. I run

df = df.round(decimals=6)

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.

How do I get rid of the trailing zeros in all the columns, once I ran pandas df.round() ?

I want to save the dataframe as a csv, and need the data to show the way I wish.

pashute
  • 3,965
  • 3
  • 38
  • 65

1 Answers1

6
df = df.round(decimals=6).astype(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
cs95
  • 379,657
  • 97
  • 704
  • 746
  • Wow!! So much talk about `fs['mycol'].as_type(str).str.replace('.0,',',')` and I even went so far as to texfile.readlines with that replace... after all else was done. This is great!! – pashute Nov 29 '17 at 12:51
  • Can you provide any explanation about _why_ Pandas renders integral floats in a dtype object series this way? Is this behaviour documented? It seems like a dangerous thing to rely on if not (and converting to `object` purely to solve a representational issue seems inadvisable). – Mark Dickinson Nov 30 '17 at 08:46
  • @MarkDickinson Not very knowledgable on the "why" bit. And yes, I usually add the obligatory disclaimer about performance, but for this question it was alright because `I want to save the dataframe as a csv, and need the data to show the way I wish.`, so it seems it was _only_ to solve a representational issue and nothing more. – cs95 Nov 30 '17 at 09:59
  • why choose 6 here? what if you just want to remove trailing .0s? – excelguy Dec 13 '19 at 18:31
  • 1
    @excelguy I chose 6 because the OP chose 6 (check the question) – cs95 Dec 13 '19 at 18:55
  • thanks, what if you dont want to display decimals and not round these? can this be done? my numbers will just have a `.0` appended. – excelguy Dec 13 '19 at 18:59
  • @excelguy Did you just want a flooring operation `df['col'] = np.floor(df['col'])`? – cs95 Dec 13 '19 at 19:03
  • no sorry, i dont want to round, i simply want to remove `.0` from end of each string. – excelguy Dec 13 '19 at 19:05
  • @excelguy ohhh, just do `df.astype(object)` it will not display the decimal point when printing. Otherwise, you can convert it to string and then strip trailing zeros via string operation. Up to you – cs95 Dec 13 '19 at 19:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/204225/discussion-between-excelguy-and-cs95). – excelguy Dec 13 '19 at 19:07
  • Hey would you mind replying here please, I'm at work and don't want to open a chatroom. @excelguy – cs95 Dec 13 '19 at 19:08
  • sure, maybe look at my question here, https://stackoverflow.com/questions/59326277/pandas-trailing-0s-on-object-column . Cant seem to get it with object or string and removal. – excelguy Dec 13 '19 at 19:10