-1

I generate a dataframe for lonlat like this

a=np.arange(89.7664, 89.7789, 1e-4)
b=np.arange(20.6897, 20.7050, 1e-4)
temp_arr=np.array(np.meshgrid(a, b)).T.reshape(-1, 2)
np_df=pd.DataFrame(temp_arr, columns = ['lon','lat'])

and it create the dataframe I want

enter image description here

When I tried to subset the first lon

len(np_df[np_df['lon']==89.7664])

it will return 153. But when I tried subset some last lon

len(np_df[np_df['lon']==89.7788])

it will return 0

I wonder what is wrong here. Thank you

d_frEak
  • 440
  • 3
  • 12

1 Answers1

1

Use numpy.isclose if compare floats within a tolerance:

len(np_df[np.isclose(np_df['lon'], 89.7788)])

If still not working integers with multiple by 10000 and cast to ints should help:

len(np_df[np_df['lon'].mul(10000).astype(int).eq(897788)])
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 1
    Thank you, I got some length but it is like 1377 data. Is it because I use float for this, so it cant compare with exactly same number? – d_frEak Feb 01 '23 at 07:29
  • 1
    @d_frEak - exactly. If still no match try change `rtol` and `atol` parameters – jezrael Feb 01 '23 at 07:32
  • oh thanks, I think I will try to change it into integer then, maybe I can get the exact number – d_frEak Feb 01 '23 at 07:35