-2

I have a script that is iterating through two specific columns in an excel file and capturing the Item ID and the Item Value in to a dictionary.

The problem I am having it that the Item IDs are floats and Python treats 1.1 the same as it would 1.10

The Item IDs are as follows:

1.1 -=> this one is problematic with 1.10
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
1.10
1.11

It then continues to the 2.x series

2.1 -=> this is treated the same as 2.10
2.2
2.3
2.4
2.5
2.6
2.7
2.8
2.9
2.10
2.11
2.12

I have tried to se them as strings to make them different from each other but that does not appear to work.

The final output form the date I compile is an excel file with the following columns:

 filename     item_id     item_value

Currently it treats 1.1 any 1.10 as 1.10 in excel - I need them to be distinct...

Anyone have any recommendations no how best to handle this?

Mad Physicist
  • 76,709
  • 19
  • 122
  • 186
wardsback
  • 107
  • 1
  • 4
    Well mathematically `1.1 === 1.10`. If you are trying to keep them as distinct, then just transform them into strings, and focus more on the problem with why that isn't working for you by showing us your code. – Sunny Patel Sep 19 '18 at 18:00
  • Your question has nothing to do with accuracy. The *number* 1.1 is equal to 1.10 as well as 1.100, 1.1000, etc. – Mad Physicist Sep 19 '18 at 18:01
  • [this](https://stackoverflow.com/questions/13962837/reading-date-as-a-string-not-float-from-excel-using-python-xlrd) might be helpful – Kevin He Sep 19 '18 at 18:01
  • 1
    make em strings man – SuperStew Sep 19 '18 at 18:02
  • 1
    You said using them as strings doesn't work. How come? In what way doesn't it work? – ubadub Sep 19 '18 at 18:03
  • sounds like an excel formatting issue not a python issue. try first writing the data to a text file until you get the expected format, then read that into excel. – davedwards Sep 19 '18 at 18:04
  • To get 1.1 to sort before 1.10 maybe you could try some sort of [version sort](https://stackoverflow.com/a/2574230/8890345) with `StrictVersion` from `distutils.version`? – Nathan Mills Sep 19 '18 at 18:06

1 Answers1

0

So, python treats both 1.1 and 1.10 and 1.10000 as the same float value with different precisions that's it.

In order for you to separate them and distinguish them your best option would be to convert the column to string. You can do that by

df['columnName'] = df['columnName'].astype(str)

In this way they are considered as strings rather than floats. They will be treated differently.

Look into this for more understanding of float types in Python Float types

Chandra Kanth
  • 366
  • 2
  • 14