Search code examples
pythonpython-3.xpandasnegative-number

Subtract 1.00 from all pandas data-frames. Getting large negative values?


I have five data frames. I want to subtract 1.00 from all dataframes containing values equal to 1 or less than 1.

DT   D1   D2
0   1.0  1.0


RE   E1   E2   E3
0   1.0  1.0  0.8


FE   F1   F2
0   1.0  0.63


SE   S1   S2
0   1.0  1.0


DT   D1             D2          
RE   E1   E2   E3   E1   E2   E3
0   1.0  1.0  1.0  1.0  0.45  1.0


DT   D1                            D2                         
RE   E1        E2        E3        E1        E2        E3     
FE   F1   F2   F1   F2   F1   F2   F1   F2   F1   F2   F1   F2
0   1.0  0.0  0.0  1.0  1.0  1.0  1.0  1.0  1.0  0.0  1.0  1.0

I tried following for all dataframes, however, in some cases I get large negative values like 1.110223e-16, -2.220446e-16. How to avoid these values?

df = 1.00 - df

Solution

  • 1.110223e-16 isn't a large, negative value. It's actually an extremely small positive value. The e-16 represents a tiny exponent.

    1e-2
    # 0.01
    
    1e-4
    # 0.0001
    
    print('{:.16f}'.format(1e-16))
    # 0.0000000000000001
    

    My guess is you're running into floating point inconsistencies. For example, the 1s in your data may actually have tiny floating point fractions:

    1 + 1.110223e-16
    # 1.0
    

    It's just that you don't get to see it when you print out your DataFrames.


    One suggestion I would have is to use np.isclose and set these near 0 values to zero.

    df.values[np.isclose(df.values, 0)] = 0