使用apply()加速数据框上的嵌套循环

时间:2019-04-16 08:56:17

标签: python-3.x pandas numpy lambda

我有一个使用Python中的Pandas的数据框,该数据框的每一行都包含纬度和经度坐标。我的目标是添加另一列名为“ close_by”的列,其中包含使用hasrsine计数的数据集中1英里内其他条目的数量。

我还看到了类似问题的其他指南,例如:https://engineering.upside.com/a-beginners-guide-to-optimizing-pandas-code-for-speed-c09ef2c6a4d6,但是它们涉及使用df.apply()更新每一行,以增加坐标和一些静态的每个定义点之间的距离。我没有任何运气找到或提出解决方案。

本质上,这就是我要优化的内容:

for index1, row1 in business_data.iterrows():
    for index2, row2 in business_data.iterrows():
        distance = mpu.haversine_distance((business_data.at[index1,'latitude'], business_data.at[index1,'longitude']), (business_data.at[index2,'latitude'], business_data.at[index2,'longitude']))
        distance = distance * 0.621371

        if distance <= 1:
            business_data.at[index1,'close_by'] = row1["close_by"] + 1

我大约有50,000行,在我的计算机上,每行大约需要5秒。

谢谢您的任何建议!

1 个答案:

答案 0 :(得分:1)

从外观上看,mpu.haversine_distance()使用math而不是numpy函数,因此它不能向量化。

使用this vectorized haversine distance function可以轻松地将问题向量化:

df = pd.DataFrame([
    {'latitude': 49.001, 'longitude': 11.0},
    {'latitude': 49.0, 'longitude': 11.0},
    {'latitude': 49.001, 'longitude': 11.001},
    {'latitude': -49.0, 'longitude': 11.0},
])


lon = df['longitude'].to_numpy()
lat = df['latitude'].to_numpy()

radius = 1.0

df['close_by'] = np.count_nonzero(haversine_np(lon, lat, lon[:, None], lat[:, None]) < radius, axis=0) - 1

df
#   latitude    longitude   nearby
# 0 49.001      11.000      2
# 1 49.000      11.000      2
# 2 49.001      11.001      2
# 3 -49.000     11.000      0
相关问题