Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在数据帧中使用geopy获取距离_Python_Pandas_Geopy - Fatal编程技术网

Python 在数据帧中使用geopy获取距离

Python 在数据帧中使用geopy获取距离,python,pandas,geopy,Python,Pandas,Geopy,我是地质学新手。我在这家运输公司工作,需要得到一辆卡车行驶的总公里数 我在这里看到了一些答案,但它们并不适用于我 我从卡车上安装的GPS中获得以下数据帧 latitude longitude 0 -25.145439 -54.294871 1 -24.144564 -54.240094 2 -24.142564 -54.198901 3 -24.140093 52.119021 第一步是制作第三列,将所有内容转换到某一点,但我所有的尝试都失败了 我写 df

我是地质学新手。我在这家运输公司工作,需要得到一辆卡车行驶的总公里数

我在这里看到了一些答案,但它们并不适用于我

我从卡车上安装的GPS中获得以下数据帧

    latitude    longitude
0   -25.145439  -54.294871
1   -24.144564  -54.240094
2   -24.142564  -54.198901
3   -24.140093  52.119021
第一步是制作第三列,将所有内容转换到某一点,但我所有的尝试都失败了

我写

df['point'] = df['latitude'].astype(float),df['longitude'].astype(float)
它返回一个对象。我希望它返回一个点。我的目标是:

    latitude    longitude      Point
0   -25.145439  -54.294871     (-25.145439  -54.294871)
1   -24.144564  -54.240094     (-24.144564  -54.240094)
2   -24.142564  -54.198901     (-24.142564  -54.198901)
3   -24.140093  52.119021      (-24.140093  52.119021)
然后我想计算一下这两个的距离,我会得到这样的结果:

    latitude    longitude      Point                        Distance KM
0   -25.145439  -54.294871     (-25.145439  -54.294871)     0
1   -24.144564  -54.240094     (-24.144564  -54.240094)     0,2
2   -24.142564  -54.198901     (-24.142564  -54.198901)     0,4
3   -24.140093  52.119021      (-24.140093  52.119021)      0,2
请注意,距离是与上一行的差值(已按顺序排列)

我正在努力:

df['distance'] = geodesic(df['point'],df['point'].shift(1))
我得到一个错误,它不能与tupple一起工作

有人知道这个问题的解决办法吗


tks

创建一个
系列:

将熊猫作为pd导入
df=pd.DataFrame(
[
(-25.145439,  -54.294871),
(-24.144564,  -54.240094),
(-24.142564,  -54.198901),
(-24.140093,  52.119021),
],
列=['纬度','经度']
)
从地质导入点
从geopy.distance导入距离
df['point']=df.apply(lambda行:点(纬度=行['latitude'],经度=行['latitude']),轴=1)
下一步添加一个新的移位
系列:

df['point_next']=df['point'].shift(1)
df.loc[df['point_next'].isna(),'point_next']=None
计算距离:

df['distance\u-km']=df.apply(lambda行:距离(行['point'],行['point\u-next'])。如果行['point\u-next']不是其他浮点('nan'),轴=1,则为km)
df=df.drop('下一个点',轴=1)

知道了。谢谢在这篇文章中:
In [2]: df
Out[2]:
    latitude  longitude                                point
0 -25.145439 -54.294871  25 8m 43.5804s S, 54 17m 41.5356s W
1 -24.144564 -54.240094  24 8m 40.4304s S, 54 14m 24.3384s W
2 -24.142564 -54.198901  24 8m 33.2304s S, 54 11m 56.0436s W
3 -24.140093  52.119021    24 8m 24.3348s S, 52 7m 8.4756s E
In [4]: df
Out[4]:
    latitude  longitude                                point                           point_next
0 -25.145439 -54.294871  25 8m 43.5804s S, 54 17m 41.5356s W                                 None
1 -24.144564 -54.240094  24 8m 40.4304s S, 54 14m 24.3384s W  25 8m 43.5804s S, 54 17m 41.5356s W
2 -24.142564 -54.198901  24 8m 33.2304s S, 54 11m 56.0436s W  24 8m 40.4304s S, 54 14m 24.3384s W
3 -24.140093  52.119021    24 8m 24.3348s S, 52 7m 8.4756s E  24 8m 33.2304s S, 54 11m 56.0436s W
In [6]: df
Out[6]:
    latitude  longitude                                point   distance_km
0 -25.145439 -54.294871  25 8m 43.5804s S, 54 17m 41.5356s W           NaN
1 -24.144564 -54.240094  24 8m 40.4304s S, 54 14m 24.3384s W    111.003172
2 -24.142564 -54.198901  24 8m 33.2304s S, 54 11m 56.0436s W      4.192654
3 -24.140093  52.119021    24 8m 24.3348s S, 52 7m 8.4756s E  10449.661388