在熊猫/python中的相对距离

发布于 2025-02-09 02:24:02 字数 260 浏览 1 评论 0原文

我有一个带有一系列位置的数据框。到目前为止,我拥有的三列是:

['Location number','x_coordinate', 'y_coordinate]

现在我想拥有一个指示距离的第四列。在上一个。

由于FIRTS位置没有前面的位置,因此我的距离不能为零。

对于另一个位置,“曼哈顿距离”应该最容易计算,并且非常适合我使用。

对于那些不熟悉的曼哈顿距离的人:delta x + delta y

I have a dataframe with a series of locations in it. The three columns i have so far are:

['Location number','x_coordinate', 'y_coordinate]

Now i would like to have a forth column which indicates the distance. between te previous one.

As the firts location does not has a preceding location, i't distance can just be zero.

For the other location, 'manhattan distance' should be most easy to compute, and is just perfect for my use.

For those not familiar to the manhattan distance: delta x + delta y

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江南烟雨〆相思醉 2025-02-16 02:24:02

/a>方法应该解决:

df['manhattan_distance'] = (
    df.x_coordinate.diff().fillna(0).abs()
    + df.y_coordinate.diff().fillna(0).abs()
)

The pd.Series.diff method should do the trick:

df['manhattan_distance'] = (
    df.x_coordinate.diff().fillna(0).abs()
    + df.y_coordinate.diff().fillna(0).abs()
)
痕至 2025-02-16 02:24:02

刚刚找到了可能工作得更好的解决方案:

import scipy.spatial

 
scipy.spatial.distance.cdist(XA, XB, metric='euclidean', *, out=None, **kwargs)

来源:

https://docs.scipy.org/doc/doc/scipy/reference/reference/generated/generated/generated/scipy.spatial.distance.cdist.cdist.html #html#scipipy.spatial.distististial.distist. CDIST

Just found a solution that might work better:

import scipy.spatial

 
scipy.spatial.distance.cdist(XA, XB, metric='euclidean', *, out=None, **kwargs)

Source:

https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.cdist.html#scipy.spatial.distance.cdist

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文