I have a set of objects and their positions over time. I would like to get the average distance between objects for each time point. An example dataframe is as follows:
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())
Outputs:
time
0 1.550094
1 10.049876
2 53.037722
dtype: float64