Euclidean distance of points coordinates in pandas dataframe

前端 未结 2 1412
旧时难觅i
旧时难觅i 2020-12-21 08:47

I have a pandas dataframe with six columns, first three columns contain x, y and z reference coordinate, and the next three - coordinates of some

相关标签:
2条回答
  • 2020-12-21 09:18

    Here's how I do it:

    p1 = df[['x1', 'y1', 'z1']]
    p2 = df[['x2', 'y2', 'z2']]
    
    df['dist'] = ((p1 - p2)**2).sum(axis=1) ** 0.5
    
    0 讨论(0)
  • 2020-12-21 09:27

    You might not need any fancy magic here:

    df['dist'] = np.sqrt( (df.x1-df.x2)**2 + (df.y1-df.y2)**2 + (df.z1-df.z2)**2)
    
    0 讨论(0)
提交回复
热议问题