0

I will need a little help on this small matters. Please let me know your thoughts on that.

In this test, we will see that '1.731' in a text file transform to '1.7309999999999999' in a dataframe.

This process is included in a larger process that compare 2 dataframe cell by cell to report any difference. So i dont want to round it, that will NOT be a solution for me

Some piece of code you can test:

# This will Work correctly
df1 = pd.DataFrame([['B',0.05,1428.6443,674,
                     1.731]], columns = range(5))

# This wont Work correctly
str_path = 'file_test.txt'
df1 = pd.read_csv(str_path, sep = '\t', header = None)

print(df1)
for i_row, t_row in df1.iterrows():
    i_col = 0
    for value in t_row:
        try:
            flt_1 = float(df1.iloc[i_row, i_col])
            if abs(flt_1 - 1.731) < 0.001:
                print('-----------------')
                print('i_row: ', i_row)
                print('value: \t', value)
                print('flt_1: \t', flt_1)
                print('iat: \t', df1.iat[i_row, i_col])
                print('iloc: \t', df1.iloc[i_row, i_col])
                print('loc: \t', df1.loc[i_row, i_col])
                print('value: \t', value)
                print('t_row: \t', t_row[i_col])
                print('t_row: \n', t_row, '\n\n')
        except: pass

        # End LOOP
        i_col += 1
        

The result I have is:

      0         1      2        3  4   ...         7  8   9    10     11
0  B  LU129109  HK008  0966.HK  -  ...  1428.6443  -   -  674  1.731

[1 rows x 12 columns]
-----------------
i_row:  0
value:   1.7309999999999999
flt_1:   1.7309999999999999
iat:     1.7309999999999999
iloc:    1.7309999999999999
loc:     1.7309999999999999
value:   1.7309999999999999
t_row:   1.7309999999999999
t_row: 
 0                                         B
1                                  LU129109
2                                     HK008
3                                   0966.HK
4                                         -
5                                      XHKG
6     surance Holdings Company ORD HKD 0.05
7                                   1428.64
8                                         -
9                                         -
10                                      674
11                                    1.731
Laurent T
  • 275
  • 3
  • 7

0 Answers0