I am very new to Python. I have DataFrame with column called quantity. I am able to read value using df['QTY']
.
I need for format value like below
0.00 -> 0
12.0 -> 12
100.00 -> 100
12.00 -> 12
12 -> 12
123.34 -> 123.34
I have tried and everyone is taking examples of xx.yy
and showing formating but I will have combination of xx.00
, xx.yy
and xx
only.
My question is different from Formatting floats in Python without superfluous zeros
There it is converting 3.140 to 3.14. In my case 3.140 must convert to 3.140 and 3.00 must convert to 3
SOLUTION:
I ended up like below
for row in df.iterrows():
if row['QTY'].is_integer():
df['QTY'] = "{:,}".format(int(row['QTY']))
else:
df['QTY'] = "{:,}".format(float(row['QTY']))
Now my df['QTY'] has all values in nicely formatted way.