首页 > 解决方案 > 从坐标表创建距离变量

问题描述

我有一张带有接送坐标的表格,并希望在数据框中使用“距离”创建一个新变量。我在堆栈上四处搜索,但没有获胜。(我是数据科学的新手 - 并复制了一些其他代码试图理解它)。

桌子:

在此处输入图像描述

下面的代码不正确,我不知道如何转换为弧度,非常感谢更好的方向。

def haversine_np(lon1, lat1, lon2, lat2):
    """
    Calculate the great circle distance between two points
    on the earth (specified in decimal degrees)

    All args must be of equal length.    

    """
    lon1, lat1, lon2, lat2 = map(np.radians, [pickup_longitude, pickup_latitude, dropoff_longitude, dropoff_latitude])

    dlon = lon2 - lon1
    dlat = lat2 - lat1

    a = np.sin(dlat/2.0)**2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon/2.0)**2

    c = 2 * np.arcsin(np.sqrt(a))
    km = 6367 * c
    return km

data['distance'] = haversine_np(data['lon1'],data['lat1'],data['lon2'],data['lat2'])

结果在此处输入图像描述

标签: python

解决方案


尽管我实际上并没有检查距离是否正确,但代码似乎对我有用。:)

import numpy as np
import pandas as pd

def haversine_np(lon1, lat1, lon2, lat2):
    """
    Calculate the great circle distance between two points
    on the earth (specified in decimal degrees)

    All args must be of equal length.

    """
    lon1, lat1, lon2, lat2 = map(np.radians, [lon1, lat1, lon2, lat2])

    dlon = lon2 - lon1
    dlat = lat2 - lat1

    a = np.sin(dlat / 2.0) ** 2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon / 2.0) ** 2

    c = 2 * np.arcsin(np.sqrt(a))
    km = 6367 * c
    return km

data = pd.DataFrame(
    {
        "pickup_longitude": [-73.953918,-73.988312,-73.997314,-73.96167,-74.01712],
        "pickup_latitude": [40.778873,40.731743,40.721458,40.75972,40.708469],
        "dropoff_longitude": [-73.963875,-73.994751,-73.948029,-73.956779,-73.988182],
        "dropoff_latitude": [40.771164,40.694931,40.774918,40.750628,40.740631],
    }
)


data["distance"] = haversine_np(
    data["pickup_longitude"],
    data["pickup_latitude"],
    data["dropoff_longitude"],
    data["dropoff_latitude"],
)

print(data)
皮卡经度 皮卡纬度 dropoff_longitude dropoff_latitude 距离
-73.953918 40.778873 -73.963875 40.771164 1.198317
-73.988312 40.731743 -73.994751 40.694931 4.126535
-73.997314 40.721458 -73.948029 40.774918 7.246234
-73.961670 40.759720 -73.956779 40.750628 1.091016
-74.017120 40.708469 -73.988182 40.740631 4.325829

推荐阅读