Python Forum
Conditional formatting in csv export
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Conditional formatting in csv export
#1
Hi. I have a dataframe containing zeros and some other numbers.
When I export it to csv I would like to have the following format:
a) non zero numbers to have 2 digits after comma like 10.56
b) zeros to be just integer 0 without anything after comma

In short now I have 10.56 and 0.00 and I want 10.56 and 0

Would you know how to tackle this? The file contains a flexible number of columns with or without zeros. Thanks.
Reply
#2
you can use formatter 'g', but this will chamge all numbers with zero exponents

example:
print(f"{5.75:g}")
print(f"{5.00:g}")
print(f"{0.00:g}")
Output:
5.75 5 0
cabtor likes this post
Reply
#3
Thanks.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020