2

I bet this question has been answered a number of times but I am struggling to find a definitive solution.

I need to delete dataframe rows based on a greater or equal condition. Because of float64 type I am not able to satisfy the "equal" part of the condition. Splitting the condition into two seems cumbersome and not very pandorable. Can someone help me with finding solution?

Thanks.

Dataframe:

      Sg    Sw        temp_S       Krg       Krw          Pc
0   0.00  1.00 -5.263158e-02  0.000000  0.650000    0.000000
1   0.05  0.95 -4.382459e-17  0.000000  0.650000    0.000000
2   0.10  0.90  5.263158e-02  0.000000  0.593548    0.095790
3   0.15  0.85  1.052632e-01  0.000000  0.537097    0.107775
4   0.20  0.80  1.578947e-01  0.000000  0.480645    0.122121
5   0.25  0.75  2.105263e-01  0.000000  0.424194    0.139496
6   0.30  0.70  2.631579e-01  0.000000  0.367742    0.160837
7   0.35  0.65  3.157895e-01  0.000000  0.311290    0.187397
8   0.36  0.64  3.263158e-01  0.000000  0.300000    0.193483
9   0.40  0.60  3.684211e-01  0.014167  0.230400    0.221009

Slicing:

print(object.sc_df[object.sc_df['Sg'].values > 0.05])

Output:

      Sg    Sw    temp_S       Krg       Krw          Pc
2   0.10  0.90  0.052632  0.000000  0.593548    0.095790
3   0.15  0.85  0.105263  0.000000  0.537097    0.107775
4   0.20  0.80  0.157895  0.000000  0.480645    0.122121
5   0.25  0.75  0.210526  0.000000  0.424194    0.139496
6   0.30  0.70  0.263158  0.000000  0.367742    0.160837
7   0.35  0.65  0.315789  0.000000  0.311290    0.187397
8   0.36  0.64  0.326316  0.000000  0.300000    0.193483
9   0.40  0.60  0.368421  0.014167  0.230400    0.221009

As you can see, line 1 is missing. What would be the best way satisfying "equal" condition?

Dmitry
  • 145
  • 9
  • You can try multiply that column by 100 and make it an int64 column, or use the condition that the the difference between the column and 0.05 has to be greater than -0.005 – Tim Dec 16 '19 at 00:40
  • 1
    You can check this thread out: https://stackoverflow.com/questions/33626443/comparing-floats-in-a-pandas-column. In my opinion, splitting condition is more pandorable than other way around like setting tolerance. – Sheng Zhuang Dec 16 '19 at 01:06
  • 1
    Thanks Sheng Zhuang, this post pointed me in the right direction. Got it done with the following slicing: print(object.sc_df[object.sc_df['Sg'].ge(0.05) | np.isclose(object.sc_df['Sg'], (0.05))]) – Dmitry Dec 16 '19 at 03:04

0 Answers0