[英]Iterative euclidean distance calculation between consecutive points (x,y tuples) which belongs to a list of lines
I have a dataframe which contains Lines, PointID, X and Y coordinates; 我有一个包含Lines,PointID,X和Y坐标的数据框; each line contains a group of points with X,Y coordinates:
每行包含一组带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
Trying to calculate the euclidean distance between consecutive points within a line to obtain as a result the following: 试图计算一条线内连续点之间的欧氏距离,以获得以下结果:
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)
My Attemp was to create a DataFrame, use groupby to group the lines 'LINE' and then calculate the euclidean distance between consecutive points within a line by using scipy: 我的Attemp是创建一个DataFrame,使用groupby对行'LINE'进行分组,然后使用scipy计算一行内连续点之间的欧氏距离:
predist = df.groupby(['LINE']).apply(lambda x: x)
dist = pdist(predist[['X', 'Y']], 'euclidean')
I'm definitely doing something wrong, as the results I'm obtaining are cumulative distances between the first point of a line with each consecutive point within a line, instead of receiving the distances between each individual segment created by consecutive points (tuple of coordinates). 我肯定做错了,因为我得到的结果是一行中第一个点与一行内每个连续点之间的累积距离,而不是接收由连续点创建的每个单独段之间的距离(坐标元组) )。
You could use shift()
to find the X
and Y
coordinates of the previous point for every point in LINE
. 您可以使用
shift()
来查找LINE
每个点的前一点的X
和Y
坐标。 Then calculate distances between this point and previous point: 然后计算此点与上一点之间的距离:
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)
This produces the expected distances: 这会产生预期的距离:
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
values can be filled in a way that fits your usecase. NaN
值可以以适合您的用例的方式填充。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.