Euclidean Distance Matrix Using Pandas

前端 未结 3 2100
温柔的废话
温柔的废话 2021-02-09 01:48

I have a .csv file that contains city, latitude and longitude data in the below format:

CITY|LATITUDE|LONGITUDE
A|40.745392|-73.978364
B|42.562786|-114.460503
C|         


        
3条回答
  •  后悔当初
    2021-02-09 02:13

    the matrix can be directly created with cdist in scipy.spatial.distance:

    from scipy.spatial.distance import cdist
    df_array = df[["LATITUDE", "LONGITUDE"]].to_numpy()
    dist_mat = cdist(df_array, df_array)
    pd.DataFrame(dist_mat, columns = df["CITY"], index = df["CITY"])
    

提交回复
热议问题