,可以通过以下步骤实现:
import pandas as pd
from scipy.spatial.distance import pdist, squareform
data = {'A': [1, 2, 3, 4, 5],
'B': [6, 7, 8, 9, 10],
'C': [11, 12, 13, 14, 15]}
df = pd.DataFrame(data)
distances = pdist(df.values)
distance_matrix = squareform(distances)
indices = distance_matrix.argsort(axis=None)[:N]
min_distance_pairs = [(i // distance_matrix.shape[0], i % distance_matrix.shape[0]) for i in indices]
完整的代码如下:
import pandas as pd
from scipy.spatial.distance import pdist, squareform
data = {'A': [1, 2, 3, 4, 5],
'B': [6, 7, 8, 9, 10],
'C': [11, 12, 13, 14, 15]}
df = pd.DataFrame(data)
distances = pdist(df.values)
distance_matrix = squareform(distances)
N = 3
indices = distance_matrix.argsort(axis=None)[:N]
min_distance_pairs = [(i // distance_matrix.shape[0], i % distance_matrix.shape[0]) for i in indices]
print(min_distance_pairs)
这段代码将从给定的pandas数据帧中计算出N个最小距离对的索引,并打印出结果。你可以根据实际需求修改数据帧和N的值。
领取专属 10元无门槛券
手把手带您无忧上云