-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?

7
  • 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. Commented Sep 19, 2018 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. Commented Sep 19, 2018 at 18:01
  • this might be helpful Commented Sep 19, 2018 at 18:01
  • 1
    make em strings man Commented Sep 19, 2018 at 18:02
  • 1
    You said using them as strings doesn't work. How come? In what way doesn't it work? Commented Sep 19, 2018 at 18:03

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.