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|
the matrix can be directly created with cdist in scipy.spatial.distance:
cdist
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"])