I have a dataframe like this
df = pd.DataFrame({
'id': ['A','A','B','B','B'],
'x': [1,1,2,2,3],
'y': [1,2,2,3,3]
})
The output I want is the average distance for each point in the group, in this example
group A: (distance((1,1),(1,2))) /1 = 1
group B: (distance((2,2),(2,3)) + distance((2,3),(3,3)) + distance((2,2),(3,3))) /3 = 1.138
I can calculate the distance using np.linalg.norm but I confused to use it in pandas groupby. Thank you
Note: 1 of my idea is I am trying to make this dataframe first (where I stuck), which is contains the pairs of point that I need to calculate the distance and after this I just need to calculate distance and groupby mean



A possible solution, based on
numpy broadcasting:Output: