Python Forum

Full Version: Conditional formatting in csv export
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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
Thanks.