Skip to content
Advertisement

Distance Matrix between rows of a Pandas Dataframe with Lat and Lon

I have a Pandas DataFrame with the coordinates of different cell towers where one column is the Latitude and another column is the Longitude like this:

JavaScript

and so on

I need to get the distances between each cell tower and all the others, and subsequently between each cell tower and its closest neighbouring tower.

I have been trying to recycle some code of the distance between the location of the tower and the expected location of a tower that I got from interpolation (in this case I had 4 different columns, 2 for the coordinates and 2 for the expected coordinates). The code I had used is the following:

JavaScript

I have not been able to now compute the distance matrix of the cell towers in the DataFrame that I have now. Can anybody help me with this one?

Advertisement

Answer

Scipy’s distance_matrix essentially uses broadcast, so here’s a solution

JavaScript

Output (c):

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement