python - 半正弦函数的 Python 数学库中出现错误

标签 python math distance haversine

我试图找到两个地理纬度和经度之间的距离

我用必要的公式创建了一个函数半正弦函数:

from math import radians,cos,sin,asin,sqrt
def haversine(lon1,lat1,lon2,lat2):
    #convert decimals to radians
    lon1,lat1,lon2,lat2=map(radians,[lon1,lat1,lon2,lat2])
    dlon = lon2 - lon1 
    dlat = lat2 - lat1 
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * asin(sqrt(a)) 
    r = 6371 # Radius of earth in kilometers. Use 3956 for miles
    return c * r

haversine(-97.481,-97.622,-74.1083,-74.106)

现在我想找到我的 df 中的点的距离,以及它们是否在泽西市经纬度 50 公里半径内:

import pandas as pd

df = pd.DataFrame({'Company':['WM','WS','WC','WW'],
                   'city':['Norman','OKC','NY','Bayonne'],
                   'latitude':[35.221,35.463,41.112,40.66],
                   'longtitude':[-97.481,-97.622,-74.1083,-74.106]})

jersey_city_lat_lon=(40.94,-74.05)
df['distance']=df.apply(haversine,args=(jersey_city_lat_lon),axis=1)
df[df['distance']<=100]

但是,当我运行上述命令时,我收到错误:

Traceback (most recent call last):

  File "<ipython-input-123-8d33df9e0fab>", line 7, in <module>
    df['distance']=df.apply(haversine,args=(jersey_city_lat_lon),axis=1)

  File "C:\Users\j\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py", line 6487, in apply
    return op.get_result()

  File "C:\Users\j\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\apply.py", line 151, in get_result
    return self.apply_standard()

  File "C:\Users\j\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\apply.py", line 257, in apply_standard
    self.apply_series_generator()

  File "C:\Users\j\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\apply.py", line 286, in apply_series_generator
    results[i] = self.f(v)

  File "C:\Users\j\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\apply.py", line 78, in f
    return func(x, *args, **kwds)

TypeError: ("haversine() missing 1 required positional argument: 'lat2'", 'occurred at index 0')

它应该可以工作并显示一个位置在纽约,另一个位置在巴约讷,它在我们正在寻找的半径内,因为纽约和巴约讷都位于距泽西城 50 公里的径向距离内。为什么我会收到此错误?

最佳答案

df.apply 将行对象(或 axis=0 的列)传递给目标函数。它不知道将行解压到您想要的字段中。您可以包装 hasrsign 函数以仅提取纬度和经度列

jersey_city_long_lat=(-74.05,40.94)
def row_hsign(row):
    return haversine(*jersey_city_long_lat,row['longtitude'],row['latitude'])

df['distance']=df.apply(row_hsign,axis=1)

关于python - 半正弦函数的 Python 数学库中出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57364818/

相关文章:

python - 如何在 PyTorch 中为预训练模型添加层?

python - 隔离林 : Categorical data

c - 在 arc4random.c 中查找 2**32 % x 的输出

java - 遍历数字中的每个数字

Python 数学 - 类型错误 : 'NoneType' object is not subscriptable

algorithm - 给定地球上 2 个接近点(<10m)的纬度/经度,我如何计算以米为单位的距离?

找不到主页的 PythonAnywhere Django 404 页面

python - 在 Python 中编辑距离

python - 有没有办法在python中测量多维空间中两个分布之间的距离?

python - 如何使用 PSF 内核(图像格式)从清晰图像创建合成模糊图像