我认为您对distance_matrix很感兴趣。
例如:
创建数据:
import pandas as pdfrom scipy.spatial import distance_matrixdata = [[5, 7], [7, 3], [8, 1]]ctys = ['Boston', 'Phoenix', 'New York']df = pd.Dataframe(data, columns=['xcord', 'ycord'], index=ctys)
输出:
xcord ycordBoston 5 7Phoenix 7 3New York 8 1
使用距离矩阵函数:
pd.Dataframe(distance_matrix(df.values, df.values), index=df.index, columns=df.index)
结果:
Boston Phoenix New YorkBoston 0.000000 4.472136 6.708204Phoenix 4.472136 0.000000 2.236068New York 6.708204 2.236068 0.000000



