0

I have a dataframe whose 1 column is of Object type and it has values like 9.04 , 9.05 which I am trying to convert string type instead of decimal as I require it in string format for further transformations. But doing so is changing the decimal numbers to long numbers.

DF['Item Updated'] = pd.DataFrame(PrelimDataDetails['Item'].astype(str))
Item Item Updated
9.02 9.02
9.03 9.03
9.04 9.04
9.05 9.049999999999999
9.06 9.059999999999999
9.07 9.069999999999999
9.08 9.079999999999998
9.09 9.089999999999998

Can someone help to suggest how to achieve this?

  • 1
    Does this answer your question? [How can I format a decimal to always show 2 decimal places?](https://stackoverflow.com/questions/1995615/how-can-i-format-a-decimal-to-always-show-2-decimal-places) – Rupti May 26 '21 at 14:46
  • 1
    Did you try using `str(PrelimDataDetails['Item'])` instead? – Lightwave May 26 '21 at 14:49
  • I have tried str function `DF['ItemUpdated']=[str(i) for i in DF['Item']]` – Vaishali Singh May 27 '21 at 05:06

1 Answers1

0

If all you want to do is create a str version limited to two decimal places you can do:

df = pd.DataFrame(data = items, columns = ['Items'])

df['Item Updated'] = df.apply(lambda row: row['Items'][:4] if row['Items'][:4][3] !='0' else row['Items'][:3], axis= 1)  

This results in the DataFrame:

    Items      Item Updated
0   9.020003    9.02
1   9.030000    9.03
2   9.100900    9.1
3   9.050000    9.05
4   9.060000    9.06
5   9.070000    9.07
6   9.080000    9.08
7   9.090000    9.09
itprorh66
  • 3,110
  • 4
  • 9
  • 21
  • Thanks for sharing this but if you look at the table which I provided, 9.05 is getting converted to 9.049999999999999 hence I cannot limit upto 2 decimal places. This is also because " 9.1 " is converting to " 9.10 " – Vaishali Singh May 27 '21 at 05:05
  • So what exactly are you looking for? – itprorh66 May 27 '21 at 13:45
  • To explain further, my source stores different items in which some are alphanumeric in nature like "A.1.1" or number like "1.03 , 1.05" , Thus this item field when importing from excel to python pandas dataframe, is of Object data type. Now there are some transformations for which I would like to convert the number 1.05 to string "1.05" but in doing so python is changing the item value from "1.05" to "1.049999999999999" . In my visualization, Item is looking weird with such long decimal number – Vaishali Singh May 27 '21 at 14:47
  • So you are telling me that you are importing data from a csv file. and you are saying that the data in the csv is showing a value like '1.03 which is being stored in the dataframe as an object data type. When you convert this object data type to a int64 float value, the number appears in the dataframe as something like 9.029999999. You want to convert the object data type to float int64 but keep the same number of decimal places as shown in the object data. Is this what you are trying to do? – itprorh66 May 27 '21 at 20:12
  • I am not converting to int64 float value but to string (str) but precisely yes the number when converting to string , is changing from 1.03 to 1.02999999999. Yes I would like to keep the same number of decimal places as of it original string value. – Vaishali Singh May 28 '21 at 04:00
  • Okay, now I understand, please see the edit to my answer for a solution – itprorh66 May 28 '21 at 12:39