lsr729
lsr729

Reputation: 832

Check if a value exists in pandas dataframe

I have a pandas dataframe which consists of 3000 latitude longitude values. I want to check if a lat-long exists in the dataframe or not.

The data frame looks like the following:

lat      long
31.76    77.84
31.77    77.84
31.78    77.84
32.76    77.85

Now, I want to check if (31.76, 77.84) exists or not in the above dataframe. If yes, then the index also.

Upvotes: 2

Views: 1135

Answers (1)

jezrael
jezrael

Reputation: 862641

Working with floats, so need numpy.isclose for check both columns, chain with & for bitwise AND and test with any for at least one True of boolean mask:

tup = (31.76, 77.84)
lat, long = tup

a = (np.isclose(df['lat'], lat) & np.isclose(df['long'], long)).any()
print (a)
True

Upvotes: 2

Related Questions