-1

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.

Ziggler
  • 3,361
  • 3
  • 43
  • 61
  • Possible duplicate of [Formatting floats in Python without superfluous zeros](https://stackoverflow.com/questions/2440692/formatting-floats-in-python-without-superfluous-zeros) – mkrieger1 Oct 05 '18 at 20:51
  • See also: https://stackoverflow.com/questions/21583758/how-to-check-if-a-float-value-is-a-whole-number – mkrieger1 Oct 05 '18 at 20:51
  • @mkrieger... I am new to python and this is my second day on python.. can you please tell how to use.. ('%f' % x).rstrip('0').rstrip('.') ... I tried this not working... print("('%f' % x).rstrip('0').rstrip('.')".format(1234.45)) – Ziggler Oct 05 '18 at 20:56
  • Can you please make that into a proper question (considering https://stackoverflow.com/help/how-to-ask)? – mkrieger1 Oct 05 '18 at 20:58
  • Please concentrate on what i need.. ( 2.0 to 2 ) , (2.1 to 2.1 ) , ( 2.10 to 2.10 )... donot remove superflous zeros like .10, .100 if .0 then remove 0 and dot. – Ziggler Oct 05 '18 at 21:06
  • Can you explain the rules by which you want to convert the numbers? Why should `2.0 to 2`, but not `2.10 to 2.1`? What should `3.140` become? – mkrieger1 Oct 05 '18 at 21:07
  • "please concentrate on..." - please **explain** better what you need. – mkrieger1 Oct 05 '18 at 21:07
  • Okay so you want to check if a float is a whole number, and if so, convert it to an integer, otherwise leave it as is. Does the second post I linked earlier help? – mkrieger1 Oct 05 '18 at 21:08
  • Using python code I am generating a report and sending that to my users through email. In that they want to see Quantity column as ( 10 ), (10.123), ( 24 ), (12,365.67 ) and so on.. From database I am getting values as 10.00 , 10.123, 24.00, 12.365.67 – Ziggler Oct 05 '18 at 21:09
  • Worked on second link.. will let you know... – Ziggler Oct 05 '18 at 21:09
  • @Ziggler maybe try this, if it is similar to your problem. [how to round/remove traling “.0” zeros in pandas column?](https://stackoverflow.com/a/42403995/1248974) – chickity china chinese chicken Oct 05 '18 at 21:09

1 Answers1

0
>>> b = 123.34
>>> print (("%%12.%df" % (0 if b == round(b) else 2)) % b).strip()
mkrieger1
  • 19,194
  • 5
  • 54
  • 65