0
$\begingroup$

I have a dataset that gives me the geographic coordinates of residential properties.

My goal is to append a new column to this dataframe that shows the properties' distances to various prominent locations in the city. I plan on doing this by finding the difference in the latitude and longitude to give X and Y values, and using the pythagorean theorem to find the straight-line distance between the two locations.

How do I structure the code so that the new column is a pythagorean function of the differences between latitude and longitude?

Thank you.

$\endgroup$
1
$\begingroup$

Use the distance matrix to know the distance from one point to all other corresponding points.

Here is an example:

def dist_PQ(P,Q):
    PP=P*P
    QQ=Q*Q
    PSquared_Sum = np.sum(PP,axis=1,keepdims=True)
    QSquared_Sum = np.sum(QQ,axis=1,keepdims=True)

    squared_distance = PSquared_Sum + QSquared_Sum.T - 2*np.dot(P,Q.T)

    return(squared_distance)
  1. Use the above function to find distances between two sets of points say (P and Q)
  2. Say P has 10 points the values are as numpy array [[x11,y11],[x12,y12],[x13,y13]...] and Q has 3 points as [[x21,y21],[x22,y22],[x23,y23]].
  3. pass P and Q to the dist_PR function to get the output as (10,3) matrix which gives the distances from all points in array P to all points of Q.

    • Now we can use the matrix of distance in our further calculation.
    • Suppose we want to find the place which is nearest from P1 (a point in P), we can simply find the minimum distance value in row number 1 of that distance matrix.
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.