2

我在使用 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()

谢谢

安迪

4

1 回答 1

3

我想我会为 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

但是,在申请中的申请仍然不会特别有效......

于 2013-04-29T11:49:19.453 回答