【问题标题】:Efficient way to do spatial anaylsis with pandas使用 pandas 进行空间分析的有效方法
【发布时间】:2013-04-29 11:03:20
【问题描述】:

我在使用 Panda 的 DataFrame 进行空间分析时遇到问题。现在我有一个超过 1000 行的 DataFrame 和列“用户”、“纬度”、“经度”。

基于此数据集,我想做一些空间分析,例如创建第四列,汇总 100 公里范围内的所有用户。

有什么方法可以有效地做到这一点?

现在我使用两个 for 循环和 geopy 以下列方式计算距离:

df_geo['Neighbors'] = 0

def getNeighbors():
    for i in df_geo.index:
        p1 = (df_geo.ix[i]['latitude'], df_geo.ix[i]['longitude'])
        count = 0
        for i2 in df_geo.index:
            p2 = Point (df_geo.ix[i2]['latitude'], df_geo.ix[i2]['longitude'])
            if geopy.distance.distance(p1, p2).km < 100 & i != i2: 
                count += 1
        df_geo.Neighbors[i] = count



getNeighbors()

谢谢

安迪

【问题讨论】:

    标签: pandas gis geopy


    【解决方案1】:

    我想我会为 Point 对象创建一个列:

    df['point'] = df.apply(lambda row: Point(row['latitude'], row['longitude']))
    

    然后执行以下操作:

    def neighbours_of(p, s):
        '''count points in s within 100km radius of p'''
        return s.apply(lambda p1: geopy.distance.distance(p, p1).km < 100).count()
    
    df['neighbours'] = df['points'].apply(lambda p: neighbours_of(p, df['points']) - 1)
    # the -1 ensures we don't include p in the count
    

    但是,apply 中的 apply 仍然不会特别有效...

    【讨论】:

      猜你喜欢
      • 2013-05-23
      • 2018-09-25
      • 1970-01-01
      • 2012-11-03
      • 2021-07-14
      • 1970-01-01
      • 2013-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多