【问题标题】:How to add the name of the nearest point?如何添加最近点的名称?
【发布时间】:2020-12-11 13:34:18
【问题描述】:

我有两个带有景点坐标的数据框并且存在。

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

如果有任何帮助,我将不胜感激!

【问题讨论】:

    标签: python pandas distance geopy


    【解决方案1】:
    attr.loc[att, 'name'] = exits.loc[distances.index(min_dist), 'exits']
    

    在for循环的末尾写下这一行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-19
      • 2017-08-02
      • 2019-05-09
      • 2017-04-08
      • 2012-10-28
      相关资源
      最近更新 更多