如何添加最近点的名称?
How to add the name of the nearest point?
我有两个包含景点坐标的数据框并且存在。
import pandas as pd
import geopy
from geopy.distance import geodesic
attr = pd.DataFrame(
{'attraction':['circuit', 'roller coaster'],
'latitude':[53.35923, 53.35958],
'longitude':[83.71394, 83.71256]})
exits = pd.DataFrame(
{'exits':['exit','exit2','exit3', 'exit4'],
'latitude':[53.35911, 53.3606, 53.35953, 53.3603],
'longitude':[83.71503, 83.71407, 83.71154, 83.71216]})
attraction latitude longitude
0 circuit 53.35923 83.71394
1 roller coaster 53.35958 83.71256
exits latitude longitude
0 exit 53.35911 83.71503
1 exit2 53.36060 83.71407
2 exit3 53.35953 83.71154
3 exit4 53.36030 83.71216
我用它来获取到最近出口的距离:
for att in attr.index:
distances = []
for ex in exits.index:
distances.append(geopy.distance.distance(attr.loc[att,
['latitude','longitude']], exits.loc[ex,['latitude','longitude']]))
min_dist = min(distances)
attr.loc[att, 'min_distance'] = min_dist
attr
attraction latitude longitude min_distance
0 circuit 53.35923 83.71394 0.07378947966924111 km
1 roller coaster 53.35958 83.71256 0.06813732373534863 km
我想添加一个包含最近出口名称的列。它必须看起来像:
attraction latitude longitude min_distance name
0 circuit 53.35923 83.71394 0.07378947966924111 km exit
1 roller coaster 53.35958 83.71256 0.06813732373534863 km exit3
如有任何帮助,我将不胜感激!
attr.loc[att, 'name'] = exits.loc[distances.index(min_dist), 'exits']
将这一行写在for循环的末尾
我有两个包含景点坐标的数据框并且存在。
import pandas as pd
import geopy
from geopy.distance import geodesic
attr = pd.DataFrame(
{'attraction':['circuit', 'roller coaster'],
'latitude':[53.35923, 53.35958],
'longitude':[83.71394, 83.71256]})
exits = pd.DataFrame(
{'exits':['exit','exit2','exit3', 'exit4'],
'latitude':[53.35911, 53.3606, 53.35953, 53.3603],
'longitude':[83.71503, 83.71407, 83.71154, 83.71216]})
attraction latitude longitude
0 circuit 53.35923 83.71394
1 roller coaster 53.35958 83.71256
exits latitude longitude
0 exit 53.35911 83.71503
1 exit2 53.36060 83.71407
2 exit3 53.35953 83.71154
3 exit4 53.36030 83.71216
我用它来获取到最近出口的距离:
for att in attr.index:
distances = []
for ex in exits.index:
distances.append(geopy.distance.distance(attr.loc[att,
['latitude','longitude']], exits.loc[ex,['latitude','longitude']]))
min_dist = min(distances)
attr.loc[att, 'min_distance'] = min_dist
attr
attraction latitude longitude min_distance
0 circuit 53.35923 83.71394 0.07378947966924111 km
1 roller coaster 53.35958 83.71256 0.06813732373534863 km
我想添加一个包含最近出口名称的列。它必须看起来像:
attraction latitude longitude min_distance name
0 circuit 53.35923 83.71394 0.07378947966924111 km exit
1 roller coaster 53.35958 83.71256 0.06813732373534863 km exit3
如有任何帮助,我将不胜感激!
attr.loc[att, 'name'] = exits.loc[distances.index(min_dist), 'exits']
将这一行写在for循环的末尾