如果给定点和每个点之间的距离(测向纬度, 液化天然气)小于或等于0.1km

2024-06-17 18:54:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我的进口 https://pypi.org/project/haversine/

from haversine import haversine, Unit

我有这个地理编码=(37.50485525623,127.04866656867)
我的数据帧看起来像:

  title created_at              lng                 lat
0   a   2019-01-16 21:21:11    127.04866656867   37.504855525623
1   b   2019-02-15 20:54:59    126.93494467808   37.558505360332
2   c   2019-02-22 17:57:02    126.8891543       37.5817986
3   d   2019-02-26 21:58:27       0                   0   
4   e   2019-02-26 21:58:55    127.06710898411   37.54394538898

对于每一行,我需要它的(lat,lng)对,并将其与上面第一句中给出的地理代码进行比较。你知道吗

我试过df.loc[haversine((df.lat,df.lng), geocode) <= 0.1, False] = True 但是这给了我

TypeError: cannot convert the series to <class 'float'>

所以我把我的数据类型改成float by:

df = df.astype({"lng": float, "lat":float})

当我使用df.d类型它正确地将lat,lng指定为float 64。但还是给了我同样的错误。你知道吗

haversine是haversine包中的一个函数 编辑:哈弗森需要两对地理编码。例如:哈弗林((lat1,lng1),(lat2,lng2))

类似的文章,但无法在我的上实现:TypeError: cannot convert the series to <class 'float'>
Type error: cannot convert the series to <class 'float'>


Tags: thetohttpsconvert编码dffloat地理
2条回答

您的函数haversine接受浮点值,但您正在将其发送给一个序列

你可以像这样把函数矢量化来传递它。然后它将为每个项调用函数并返回数组

如果你使用矢量化然后它将更有效,因为numba将编译python函数

v_haversine = pd.np.vectorize(lambda x,y: haversine(x,y, geocode))

df.loc[haversine(df.lat,df.lng) <= 0.1]

您可以对两列使用apply,并过滤结果:

filtered = df[(df[['lat','lng']].apply(lambda x: haversine(*x,*geocode), axis=1))<=0.1]

print (filtered)

结果:

          title created_at         lng        lat
0 a  2019-01-16   21:21:11  127.048667  37.504856

相关问题 更多 >