![](/img/trans.png)
[英]Calculating euclidean distance between consecutive points of an array with numpy
[英]Iterative euclidean distance calculation between consecutive points (x,y tuples) which belongs to a list of lines
我有一個包含Lines,PointID,X和Y坐標的數據框; 每行包含一組帶X,Y坐標的點:
LINE Point ID X coordinate Y Coordinate
A 1 1 2
A 2 2 2
A 3 3 2
B 1 11 3
B 2 12 3
B 3 13 3
試圖計算一條線內連續點之間的歐氏距離,以獲得以下結果:
LINE Point ID X coordinate Y Coordinate Euclidean Dist.
A 1 1 2
A 2 2 2 1 (dist between Point ID's 1 and 2 for line A)
A 3 3 2 1 (dist between Point ID's 2 and 3 for line A)
B 1 11 3
B 2 12 3 1 (dist between Point ID's 1 and 2 for line B)
B 3 13 3 1 (dist between Point ID's 2 and 3 for line B)
我的Attemp是創建一個DataFrame,使用groupby對行'LINE'進行分組,然后使用scipy計算一行內連續點之間的歐氏距離:
predist = df.groupby(['LINE']).apply(lambda x: x)
dist = pdist(predist[['X', 'Y']], 'euclidean')
我肯定做錯了,因為我得到的結果是一行中第一個點與一行內每個連續點之間的累積距離,而不是接收由連續點創建的每個單獨段之間的距離(坐標元組) )。
您可以使用shift()
來查找LINE
每個點的前一點的X
和Y
坐標。 然后計算此點與上一點之間的距離:
import pandas as pd
import numpy as np
data = """
LINE PointID X Y
A 1 1 2
A 2 2 2
A 3 3 2
B 1 11 3
B 2 12 3
B 3 13 3"""
df = pd.read_csv(StringIO(data),sep="\s+")
dx = (df['X'] - df.groupby('LINE')['X'].shift())
dy = (df['Y'] - df.groupby('LINE')['Y'].shift())
df['dist'] = np.sqrt(dx**2 + dy**2)
這會產生預期的距離:
LINE PointID X Y dist
0 A 1 1 2 NaN
1 A 2 2 2 1.0
2 A 3 3 2 1.0
3 B 1 11 3 NaN
4 B 2 12 3 1.0
5 B 3 13 3 1.0
NaN
值可以以適合您的用例的方式填充。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.