我有点卡住了。我有一个 CSV 文件,其中包括:
站点名称 纬度 经度。
此 CSV 有 100,000 个位置。我需要为每个位置生成一个逗号分隔的列表,显示 5KM 内的其他位置
我已经尝试了附件,它转置了表格并为我提供了 100,000 列和 100,000 行以及作为结果填充的距离。但我不知道如何制作一个新的 pandas 列,其中包含 5KM 内所有站点的列表。
你能帮忙吗?
from geopy.distance import geodesic
def distance(row, csr):
lat = row['latitude']
long = row['longitude']
lat_long = (lat, long)
try:
return round(geodesic(lat_long, lat_long_compare).kilometers,2)
except:
return 9999
for key, value in d.items():
lat_compare = value['latitude']
long_compare = value['longitude']
lat_long_compare = (lat_compare, long_compare)
csr = key
df[key] = df.apply([distance, csr], axis=1)
一些示例数据可以是:
destinations = { 'bigben' : {'latitude': 51.510357,
'longitude': -0.116773},
'heathrow' : {'latitude': 51.470020,
'longitude': -0.454295},
'alton_towers' : {'latitude': 52.987662716,
'longitude': -1.888829778}
}
bigben距离伦敦眼0.8公里 希思罗机场距伦敦眼 23.55KM alton_towers 距伦敦眼 204.63KM
因此,在这种情况下,该字段应该只显示大本钟。
所以我们得到:
网站 | 5KM以内站点 28、大本钟
最佳答案
这是使用 NearestNeighbors 的一种方法.
from sklearn.neighbors import NearestNeighbors
# data from your input
df = pd.DataFrame.from_dict(destinations, orient='index').rename_axis('Site Name').reset_index()
radius = 50 #change to whatever, in km
# crate the algo with the raidus and the metric for geospatial distance
neigh = NearestNeighbors(radius=radius/6371, metric='haversine')
# fit the data in radians
neigh.fit(df[['latitude', 'longitude']].to_numpy()*np.pi/180)
# extract result and transform to get the expected output
df[f'Site_within_{radius}km'] = (
pd.Series(neigh.radius_neighbors()[1]) # get a list of index for each row
.explode()
.map(df['Site Name']) # get the site name from row index
.groupby(level=0) # transform back to row-row relation
.agg(list) # can use ', '.join instead of list
)
print(df)
Site Name latitude longitude Site_within_50km
0 bigben 51.510357 -0.116773 [heathrow]
1 heathrow 51.470020 -0.454295 [bigben]
2 alton_towers 52.987663 -1.888830 [nan]
关于python-3.x - 查找 latlong 之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67720673/