我正在寻找以下数据中当前点与下一个端点之间的距离。单个键可以具有多个端点(x,y)。
我正在寻求比上述解决方案更快的解决方案,因为它要花很多小时才能执行。
数据Key x y
1 87.4375 28.921875
1 97.4375 23.921875
1 97.4375 23.921875
1 97.4375 23.921875
1 97.4375 23.921875
2 86.4375 24.921875
2 85.4375 24.921875
2 93.4375 29.921875
2 86.4375 24.921875
2 85.4375 24.921875
2 93.4375 29.921875
我尝试过的:
new_df= pd.DataFrame(columns=['key', 'distance'])
for key in orig_df.key.values:
x2 = orig_df.loc[orig_df.key== key ,'x'].shift(-1)
y2 = orig_df.loc[orig_df.key== key ,'y'].shift(-1)
x1 = orig_df.loc[orig_df.key== key ,'x']
y1 = orig_df.loc[orig_df.key== key ,'y']
distance = cal_distance(x2,y2,x1,y1)
new_df.append({'key': key, 'distance': distance}, ignore_index=True)
def cal_distance(x2,y2,x1,y1):
return abs(np.sqrt( np.square(x2 - x1) + np.square(y2 - y1))).sum()
行很多,执行上述代码要花费几个小时。
解决方案2(无效)
orig_df.groupby('key').pipe(cal_distance(orig_df.x.shift(-1),orig_df.y.shift(-1),orig_df.x,orig_df.y))
我尝试根据密钥进行分组。
非常感谢您的帮助。
我正在寻找以下数据中当前点与下一个端点之间的距离。单个键可以具有多个端点(x,y)。我正在寻找一种比...