0

I am working with dataframes read from csv file using pandas. I wanted to do some equality operation to find the rows where the column is of a specific value. However, it seems that the equality operation works for some numbers but not for others.

sample[sample['Time'] == 34200.045021468].index
Out[68]: Int64Index([], dtype='int64')

sample.loc[40, ['Time']]

Out[69]: 
Time    34200.045021468
Name: 40, dtype: float64
sample[sample['Time'] == 34200.647527227].index
Out[71]: Int64Index([170, 171, 172, 173, 174], dtype='int64')

I have not tried for all values but when I tried to search for the value 34,200.045021468, it did not find anything but as can be seen it does exist in my dataframe.

On the other hand, when I do the same operation for another value, 34,200.647527227, it works?!

Can anyone kindly advise on what is wrong and how it can be fixed? Thank you in advance

  • Can you share the trot you are getting? You probably need to enforce all values are numeric first using pd.to_numeric() – Amir Imani Mar 25 '19 at 12:09
  • Hi Amirhos, sorry but what is trot? – user8465900 Mar 25 '19 at 12:12
  • Sorry autocorrect - i meant the error* you are getting... – Amir Imani Mar 25 '19 at 12:19
  • Ah I see. There was no error, just that the result was empty when I am expecting it to return the indices of the rows where column value = 34200.045021468. As it correctly did when I tried using 34200.647527227 instead – user8465900 Mar 25 '19 at 12:22
  • 2
    Hmm, this really looks like an avatar of [Is floating point broken](https://stackoverflow.com/q/588004/3545273). The `Time` column is a float64, which is only an approximation of the decimal value that you can see and type. The only rule is *never use exact equality with floating point types*.. – Serge Ballesta Mar 25 '19 at 13:26

0 Answers0