Let's say you have a distance function dist that you want to minimize:
def dist(lat1, long1, lat2, long2): return np.abs((lat1-lat2)+(long1-long2))
For a given location, you can find the nearest station as follows:
lat = 39.463744 long = -76.119411 weather.apply( lambda row: dist(lat, long, row['Latitude'], row['Longitude']), axis=1)
This will allow you to calculate the distance to all weather stations. Using idxmin , you can find the name of the nearest station:
distances = weather.apply( lambda row: dist(lat, long, row['Latitude'], row['Longitude']), axis=1) weather.loc[distances.idxmin(), 'StationName']
Put all this in a function:
def find_station(lat, long): distances = weather.apply( lambda row: dist(lat, long, row['Latitude'], row['Longitude']), axis=1) return weather.loc[distances.idxmin(), 'StationName']
Now you can get all the nearest stations by applying it to the locations dataframe:
locations.apply( lambda row: find_station(row['Latitude'], row['Longitude']), axis=1)
Output:
0 WALTHAM 1 WALTHAM 2 PORTST.LUCIE 3 WALTHAM 4 PORTST.LUCIE