- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是数据框中位于纬度列名称下方的位置。我想在一个单独的数据框中显示离最近火车站的纬度有多远。
因此,例如,我的纬度为(37.814563 144.970267),而我的其他地理空间点列表如下。我想找到最接近的点,然后找到这些点之间的距离,作为郊区数据框中的一个额外列。
这是火车数据集的示例
<bound method NDFrame.to_clipboard of STOP_ID STOP_NAME LATITUDE \
0 19970 Royal Park Railway Station (Parkville) -37.781193
1 19971 Flemington Bridge Railway Station (North Melbo... -37.788140
2 19972 Macaulay Railway Station (North Melbourne) -37.794267
3 19973 North Melbourne Railway Station (West Melbourne) -37.807419
4 19974 Clifton Hill Railway Station (Clifton Hill) -37.788657
LONGITUDE TICKETZONE ROUTEUSSP \
0 144.952301 1 Upfield
1 144.939323 1 Upfield
2 144.936166 1 Upfield
3 144.942570 1 Flemington,Sunbury,Upfield,Werribee,Williamsto...
4 144.995417 1 Mernda,Hurstbridge
geometry
0 POINT (144.95230 -37.78119)
1 POINT (144.93932 -37.78814)
2 POINT (144.93617 -37.79427)
3 POINT (144.94257 -37.80742)
4 POINT (144.99542 -37.78866) >
这是郊区的一个例子
<bound method NDFrame.to_clipboard of postcode suburb state lat lon
4901 3000 MELBOURNE VIC -37.814563 144.970267
4902 3002 EAST MELBOURNE VIC -37.816640 144.987811
4903 3003 WEST MELBOURNE VIC -37.806255 144.941123
4904 3005 WORLD TRADE CENTRE VIC -37.822262 144.954856
4905 3006 SOUTHBANK VIC -37.823258 144.965926>
我要显示的是,在郊区列表的新列中,从latlon到壁橱火车站的距离。
from sklearn.neighbors import NearestNeighbors
from haversine import haversine
NN = NearestNeighbors(n_neighbors=1, metric='haversine')
NN.fit(trains_shape[['LATITUDE', 'LONGITUDE']])
indices = NN.kneighbors(df_complete[['lat', 'lon']])[1]
indices = [index[0] for index in indices]
distances = NN.kneighbors(df_complete[['lat', 'lon']])[0]
df_complete['closest_station'] = trains_shape.iloc[indices]['STOP_NAME'].reset_index(drop=True)
df_complete['closest_station_distances'] = distances
print(df_complete)
这里的输出,
<bound method NDFrame.to_clipboard of postcode suburb state lat lon Venues Cluster \
1 3040 aberfeldie VIC -37.756690 144.896259 4.0
2 3042 airport west VIC -37.711698 144.887037 1.0
4 3206 albert park VIC -37.840705 144.955710 0.0
5 3020 albion VIC -37.775954 144.819395 2.0
6 3078 alphington VIC -37.780767 145.031160 4.0
#1 #2 #3 \
1 Café Electronics Store Grocery Store
2 Fast Food Restaurant Café Supermarket
4 Café Pub Coffee Shop
5 Café Fast Food Restaurant Grocery Store
6 Café Park Bar
#4 ... #6 \
1 Coffee Shop ... Bakery
2 Grocery Store ... Italian Restaurant
4 Breakfast Spot ... Burger Joint
5 Vietnamese Restaurant ... Pub
6 Pizza Place ... Vegetarian / Vegan Restaurant
#7 #8 #9 \
1 Shopping Mall Japanese Restaurant Indian Restaurant
2 Portuguese Restaurant Electronics Store Middle Eastern Restaurant
4 Bar Bakery Gastropub
5 Chinese Restaurant Gym Bakery
6 Italian Restaurant Gastropub Bakery
#10 Ancestry Cluster ClosestStopId \
1 Greek Restaurant 8.0 20037
2 Convenience Store 5.0 20032
4 Beach 6.0 22180
5 Convenience Store 5.0 20004
6 Coffee Shop 5.0 19931
ClosestStopName \
1 Essendon Railway Station (Essendon)
2 Glenroy Railway Station (Glenroy)
4 Southern Cross Railway Station (Melbourne City)
5 Albion Railway Station (Sunshine North)
6 Alphington Railway Station (Alphington)
closest_station closest_station_distances
1 Glenroy Railway Station (Glenroy) 0.019918
2 Southern Cross Railway Station (Melbourne City) 0.031020
4 Alphington Railway Station (Alphington) 0.023165
5 Altona Railway Station (Altona) 0.005559
6 Newport Railway Station (Newport) 0.002375
和第二个功能。
def ClosestStop(r):
# Cartesin Distance: square root of (x2-x2)^2 + (y2-y1)^2
distances = ((r['lat']-StationDf['LATITUDE'])**2 + (r['lon']-StationDf['LONGITUDE'])**2)**0.5
# Stop with minimum Distance from the Suburb
closestStationId = distances[distances == distances.min()].index.to_list()[0]
return StationDf.loc[closestStationId, ['STOP_ID', 'STOP_NAME']]
df_complete[['ClosestStopId', 'ClosestStopName']] = df_complete.apply(ClosestStop, axis=1)
这奇怪地给出了不同的答案,使我认为这段代码有问题。 KM似乎也错了。
最佳答案
一些关键概念
foo=1
的方法)sort_values()
查找最小距离groupby()
和agg()
,以获得最短距离的第一个值dfdist
包含所有组合和距离dfnearest
包含结果dfstat = pd.DataFrame({'STOP_ID': ['19970', '19971', '19972', '19973', '19974'],
'STOP_NAME': ['Royal Park Railway Station (Parkville)',
'Flemington Bridge Railway Station (North Melbo...',
'Macaulay Railway Station (North Melbourne)',
'North Melbourne Railway Station (West Melbourne)',
'Clifton Hill Railway Station (Clifton Hill)'],
'LATITUDE': ['-37.781193',
'-37.788140',
'-37.794267',
'-37.807419',
'-37.788657'],
'LONGITUDE': ['144.952301',
'144.939323',
'144.936166',
'144.942570',
'144.995417'],
'TICKETZONE': ['1', '1', '1', '1', '1'],
'ROUTEUSSP': ['Upfield',
'Upfield',
'Upfield',
'Flemington,Sunbury,Upfield,Werribee,Williamsto...',
'Mernda,Hurstbridge'],
'geometry': ['POINT (144.95230 -37.78119)',
'POINT (144.93932 -37.78814)',
'POINT (144.93617 -37.79427)',
'POINT (144.94257 -37.80742)',
'POINT (144.99542 -37.78866)']})
dfsub = pd.DataFrame({'id': ['4901', '4902', '4903', '4904', '4905'],
'postcode': ['3000', '3002', '3003', '3005', '3006'],
'suburb': ['MELBOURNE',
'EAST MELBOURNE',
'WEST MELBOURNE',
'WORLD TRADE CENTRE',
'SOUTHBANK'],
'state': ['VIC', 'VIC', 'VIC', 'VIC', 'VIC'],
'lat': ['-37.814563', '-37.816640', '-37.806255', '-37.822262', '-37.823258'],
'lon': ['144.970267', '144.987811', '144.941123', '144.954856', '144.965926']})
import geopy.distance
# cartesian product so we get all combinations
dfdist = (dfsub.assign(foo=1).merge(dfstat.assign(foo=1), on="foo")
# calc distance in km between each suburb and each train station
.assign(km=lambda dfa: dfa.apply(lambda r:
geopy.distance.geodesic(
(r["LATITUDE"],r["LONGITUDE"]),
(r["lat"],r["lon"])).km, axis=1))
# reduce number of columns to make it more digestable
.loc[:,["postcode","suburb","STOP_ID","STOP_NAME","km"]]
# sort so shortest distance station from a suburb is first
.sort_values(["postcode","suburb","km"])
# good practice
.reset_index(drop=True)
)
# finally pick out stations nearest to suburb
# this can easily be joined back to source data frames as postcode and STOP_ID have been maintained
dfnearest = dfdist.groupby(["postcode","suburb"])\
.agg({"STOP_ID":"first","STOP_NAME":"first","km":"first"}).reset_index()
print(dfnearest.to_string(index=False))
dfnearest
输出
postcode suburb STOP_ID STOP_NAME km
3000 MELBOURNE 19973 North Melbourne Railway Station (West Melbourne) 2.564586
3002 EAST MELBOURNE 19974 Clifton Hill Railway Station (Clifton Hill) 3.177320
3003 WEST MELBOURNE 19973 North Melbourne Railway Station (West Melbourne) 0.181463
3005 WORLD TRADE CENTRE 19973 North Melbourne Railway Station (West Melbourne) 1.970909
3006 SOUTHBANK 19973 North Melbourne Railway Station (West Melbourne) 2.705553
减小测试组合大小的方法
# pick nearer places, based on lon/lat then all combinations
dfdist = (dfsub.assign(foo=1, latr=dfsub["lat"].round(1), lonr=dfsub["lon"].round(1))
.merge(dfstat.assign(foo=1, latr=dfstat["LATITUDE"].round(1), lonr=dfstat["LONGITUDE"].round(1)),
on=["foo","latr","lonr"])
# calc distance in km between each suburb and each train station
.assign(km=lambda dfa: dfa.apply(lambda r:
geopy.distance.geodesic(
(r["LATITUDE"],r["LONGITUDE"]),
(r["lat"],r["lon"])).km, axis=1))
# reduce number of columns to make it more digestable
.loc[:,["postcode","suburb","STOP_ID","STOP_NAME","km"]]
# sort so shortest distance station from a suburb is first
.sort_values(["postcode","suburb","km"])
# good practice
.reset_index(drop=True)
)
关于Python位置,显示与最近位置的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63300859/
我需要获取过去 24 小时的记录,但不能像这样按小时分组: SELECT HOUR(CompDate) AS hour, COUNT(1) AS action FROM mytable WHERE (
我们有一个自动完成列表,当您向某人发送电子邮件时会填充该列表,这一切都很好,直到列表变得非常大,您需要输入越来越多的地址才能找到您想要的地址, 这违背了自动完成的目的 我在想应该添加一些逻辑,以便自动
我在 android 的锁屏上工作我们如何禁用导航软按钮,已经尝试了所有方法,systemoverlay但它不起作用,在按下主页按钮时它会终止服务和 Activity 。 最佳答案 后退按钮可以通过覆
我有一个报告创建时间为 2016-05-30,现在我需要从报告时间开始的最后 7 天。我怎样才能使用时刻? report_create_time = moment('2016-05-30').form
我想找出向量中最接近的三个数字。 就像是 v = c(10,23,25,26,38,50) c = findClosest(v,3) c 23 25 26 我试过 sort(colSums(as.ma
考虑以下表结构: id speed 1 100 2 200 3 300 4 400 5 500 考虑以下查询:"SELECT * FROM records WHERE
我正在开发一个依赖 YouTube 直播和实时聊天(也来自 YouTube)的网络应用。事情进展顺利,突然嵌入的聊天功能无法在移动设备上运行。 我试图在我这边找到一个错误或一些无效的配置,但我找不到。
我正在制作一个 React Native 应用程序,它有一个安全部分,用户必须在其中输入密码才能解锁 protected 内容。问题是,当用户在锁定该部分之前切换到另一个应用程序时,将生成屏幕截图以及
我有一条 SQL 语句 (SQL Server Management Studio),我通过仪表板软件将数据传递到 where 语句中。用户可以选择年份(2013 年或现在的 2014 年)和月份(作
我有一个脚本可以添加一组行,使您能够在 SharePoint 列表表单中捕获其他访问者的信息。我有两个 anchor 标记,一个用于添加,另一个用于删除。当我添加一个新的访问者时它有效,当我删除访问者
我正在学习斯坦福 iOS 类(class),我有一个问题,我认为与最近的更新有关。这是代码部分: func evaluate(ops: [Op]) -> (result: Double?,remain
我注意到我的应用有一个奇怪的行为。每当我按下主页按钮时,我的应用程序就会被杀死。我没有在应用程序堆栈中看到该应用程序。我可以看到之前启动的其他应用程序。最初我怀疑 android:launchMode
我需要获取过去 7 天内的所有付费和临时条目,但我总是收到所有退回的内容。我不确定我做错了什么,我已经阅读了这里的很多帖子,但无法理解它是什么。 MySQL 5.6(如果它与我一直在做的事情有什么不同
我的表有一列以 mysql time() 格式格式化。 当它是一个值分配给名为 $preRemainOt 的 php 变量时我想重新安排到最近的 15 分钟 function roundTime($w
我想获取过去 7 天内每天每个产品的最后时间戳。数据库中有数千条记录。我怎样才能通过查询来做到这一点。大约有 25 种不同的产品,每种产品每天大约有 50 个时间戳。 表:构建数据 'Timesta
我现在的代码正在从 SQL 中获取移动应用程序中的数据,首先添加显示,我需要将其设置为在我的 Android 应用程序中显示最后添加的第一个。我有如下所示的 api 代码,最新的是根据我的要求显示的,
我有一张 table ,说 table 的描述为: | ID | SNO | c1 | c2 | c3 | ___________________________________ |
我有两个大小相等的向量,例如 A=[2.29 2.56 2.77 2.90 2.05] and B=[2.34 2.62 2.67 2.44 2.52]. 我有兴趣在两个相同大小的向量 A 和 B 中
之前,我在这里发布了一个问题,询问有关如何从驱动器读取和写入数据的建议,而不是通过像“aaa.txt”这样的文件标签,而只是扇区..我被建议尝试阅读和写作....但新问题出现了……毛茸茸的参数 int
我想删除在给定时间段内未登录的用户,但我稍后会根据结果选择时间段。 所以我需要报告,其中我将收到过去 1 个月、2 个月...... n 个月内未登录的用户数量。 我不太清楚如何在单个 mysql 查
我是一名优秀的程序员,十分优秀!