Conversion of numbers in Python

Issue

Iam working with a project that is converting invoice PDFs to excels and comparing values between two different columns, but iam having trouble with something when comparing values, I put the differences into a list, but when i do that python converts numbers like 0.00, 857.60, 36.50 to 0.0 ,857.6 ,36.5 and because of that i cant use .iloc from pandas to locate the values of the columns, because of it removing that last digit it doesn’t match the column value, like the column have the value 0.00, but the list has the value 0.0

this is the code to compare two values and append differences to a list

for item in values_bank:
  if item not in values_system:
     list_difference.append(item)

And this is the .loc to create an dataframe using the matches to the "list_difference"

diferencce = df_bank.loc[df_bank['value'].isin(list_difference)]

df_bank:

           Date             Name                   Value
0    25/02/2022             Lorem  C               0.00
1    02/03/2022            Ipsum   C               100.00
2    02/03/2022          Lorem Ipsum   *           16.90
3    02/03/2022            Lorem  C                2454.00
4    02/03/2022            Ipsum                   3732.00

printing only the column values:

0          0.00
1        100.00
2         16.90
3       2454.00
4       3732.00

list_difference:

['0.0', '16.9', '2454.0', '3732.0']

Edited to add list_difference and df_bank

Solution

Convert the data type of item while appending to the list:

for item in values_bank:
  if item not in values_system:
     list_difference.append(float(item))

In this way, list_difference will contain floats and the rest of your code should produce the expected outcome. An important caveat to note here is that this type casting operation can result in a value error if the item variable contains something that can’t be converted to float.

Answered By – A.T.B

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published