python - 从坐标表创建距离变量
问题描述
我有一张带有接送坐标的表格,并希望在数据框中使用“距离”创建一个新变量。我在堆栈上四处搜索,但没有获胜。(我是数据科学的新手 - 并复制了一些其他代码试图理解它)。
桌子:
下面的代码不正确,我不知道如何转换为弧度,非常感谢更好的方向。
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'])
解决方案
尽管我实际上并没有检查距离是否正确,但代码似乎对我有用。:)
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 |
推荐阅读
- powershell - 无法将 EXE 输出重定向到文件
- javascript - 如何从 javascript 中的 html 对象访问父文档?
- java - 在书签中保存自定义页码标签
- angular - 如何仅在用户下载应用程序后获取设备令牌?
- java - 我可以通过将 ssl 证书添加到我的 JVM 信任库来使用 https 网络服务
- node.js - Passport.js SSO - 验证用户是否仍登录到 IdP
- java - 有没有更优雅的方式来启动基于列表的线程?
- azure - 如何从 Azure DevOps 创建 Epic 和功能文档
- time-complexity - 为什么 B 树的复杂度是 O(log n),它不是二叉树
- c# - C# 程序集绑定重定向 - Newtonsoft.Json