且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 pandas 数据框中将欧几里德距离函数应用于groupby对象?

更新时间:2022-01-04 09:56:03

您可以将点的数组传递到scipy.spatial.distaince.pdist,它将为i> j计算Xi和Xj之间的所有成对距离.然后取平均值.

You could pass an array of the points to scipy.spatial.distaince.pdist and it will calculate all pair-wise distances between Xi and Xj for i>j. Then take the mean.

import numpy as np
from scipy import spatial

df.groupby('time').apply(lambda x: spatial.distance.pdist(np.array(list(zip(x.x, x.y)))).mean())

输出:

time
0     1.550094
1    10.049876
2    53.037722
dtype: float64