subset fail on np.meshgrid generated dataframe

Question:

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

Asked By: d_frEak

||

Answers:

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)])
Answered By: jezrael
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.