python - 如何计算python中组内两点之间的距离,对于许多后续点

标签 python python-3.x pandas

我有以下 df

       id             xx            yy  time
0       1  553343.041098  4.178420e+06     1
1       1  553343.069815  4.178415e+06     2
2       1  553343.069815  4.178415e+06     3
3       2  553343.950755  4.178415e+06     1
4       2  553341.343829  4.178410e+06     6
xxyy是每个id的位置在 time 中的某个点.

我想在此 df 中创建一个额外的列,这将是从一个时间点到另一个时间点的距离差(从 time 的最小值到下一个更大的,到下一个更大的等等),在 id 内团体。

有这样做的 Pythonic 方式吗?

最佳答案

你可以像下面这样做。

我没有做df['distance_meters']因为它是直截了当的。

df['xx_diff']=df.groupby('id')['xx'].diff()**2
df['yy_diff']=df.groupby('id')['yy'].diff()**2

如果您的数据框中不需要 ['xx_diff'] & ['yy_diff'] 列,则可以直接使用下面的代码。
df['distance']= np.sqrt(df.groupby('id')['xx'].diff()**2+df.groupby('id')['yy'].diff()**2)

输出
    id            xx         yy time    xx_diff3    yy_diff3    distance
0   1   553343.041098   4178420.0   1   NaN            NaN      NaN
1   1   553343.069815   4178415.0   2   0.000825       25.0     5.000082
2   1   553343.069815   4178415.0   3   0.000000       0.0      0.000000
3   2   553343.950755   4178415.0   1   NaN            NaN      NaN
4   2   553341.343829   4178410.0   6   6.796063      25.0      5.638800

关于python - 如何计算python中组内两点之间的距离,对于许多后续点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60224494/

相关文章:

python - 如何在 Python 单元测试中模拟一个类?

python - 缺乏对多变量赋值Python的理解

python - 如何监控传出的 HTTPS

Python 2/3 subprocess.Popen和非ascii字符

python - 在最小值之前的每列值中找到最大值

python - Pandas:如何检查索引中的任何行是否满足条件

python - 从列表构造一个字典,其中每个值将包含当前键的补码

python - 将实体在 Python Cloud Datastore 上持续变慢

Django Haystack ElasticSearch Python3

python - Python中的高维数据结构