import pandas as pdpoints = pd.DataFrame({"ID": [1, 2], "x": [4, 5], "y": [5, 6]})circle = pd.DataFrame({"ID": [1, 2], "x": [2, 1], "y": [1, 2]}) 為了得到(點、圓)的所有組合,我們可以進行交叉連接。 new_df = points.merge(circle, how='cross', suffixes=["_point", "_circle"])new_df ID_point x_point y_point ID_circle x_circle y_circle0 1 4 5 1 2 11 1 4 5 2 1 22 2 5 6 1 2 13 2 5 6 2 1 2 這樣,我們可以在每行級別將一個點與一個圓進行比較。我們在行級別使用apply(axis=1)。計算距離并將其添加為新列。 import math# this is a Euclidean distance function (feel free to change it to suit your need)def get_distance_function(x1, y1, x2, y2): return math.sqrt((x1-x2)**2 + (y1-y2)**2)new