-1

I am struggling with the implementation of my idea to create the matrix of distances in km between EACH pair of stations in NJ Transit Rail. I have data for longitude and latitude of each stop,

enter image description here

And what I want to do is this table.

enter image description here I already looked at some possible ways to achieve this table, but they are more for geometric coordinates rather than geographical. I would use the distance matrix but as I can see it is for another type of coordinates.

Help, please!

ZheniaMagic
  • 119
  • 7
  • 1
    Can you attach actual data that can be copied and not an image? – NYC Coder May 11 '20 at 19:15
  • Look at this question, it has all the details you need: [distance-matrix](https://stackoverflow.com/questions/29481485/creating-a-distance-matrix/45834105) – NYC Coder May 11 '20 at 19:18
  • Can you be more specific about which part you’re struggling with? – AMC May 11 '20 at 19:19

2 Answers2

3

You are looking for geographical distance, i.e. Haversine distance. From sklearn 0.22.2, you can use haversine_distance:

x_deg = df[['stop_lat','stop_lon']].values
x_rad = np.deg2rad(x_deg)

# distances in miles
distances = haversine_distances(x_rad,x_rad) * 6371 / 1.6

pd.DataFrame(distances, 
             index=df.stop_name, columns=df.stop_name)

and you would get something like this:

stop_name         a         b         c
stop_name                              
a          0.000000  1.274556  1.396741
b          1.274556  0.000000  0.871521
c          1.396741  0.871521  0.000000
halfer
  • 19,824
  • 17
  • 99
  • 186
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
0

You'd do it like this:

import pandas as pd
from scipy.spatial import distance_matrix

# You already have the data
data = [[5, 7], [7, 3], [8, 1]]
ctys = ['Boston', 'Phoenix', 'New York']

df = pd.DataFrame(data, columns=['xcord', 'ycord'], index=ctys)
df_mat = pd.DataFrame(distance_matrix(df.values, df.values), index=df.index, columns=df.index)
print(df_mat)

            Boston   Phoenix  New York
Boston    0.000000  4.472136  6.708204
Phoenix   4.472136  0.000000  2.236068
New York  6.708204  2.236068  0.000000
NYC Coder
  • 7,424
  • 2
  • 11
  • 24