I have two DataFrame containing Lat and Lon. I want to find distance from one (Lat, Lon)
pair to ALL (Lat, Lon)
from another DataFrame and get the minimum. The package that I am using geopy
. The code is as follows:
from geopy import distance
import numpy as np
distanceMiles = []
count = 0
for id1, row1 in df1.iterrows():
target = (row1["LAT"], row1["LON"])
count = count + 1
print(count)
for id2, row2 in df2.iterrows():
point = (row2["LAT"], row2["LON"])
distanceMiles.append(distance.distance(target, point).miles)
closestPoint = np.argmin(distanceMiles)
distanceMiles = []
The problem is that df1
has 168K
rows and df2
has 1200
rows. How do I make it faster?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…