python - 列之间的计算(经度/纬度)非常慢

标签 python pandas math multiple-columns latitude-longitude

我有两个独立的数据集,dfdf2,每个数据集都有 longitudelatitude 列。我想做的是在 df 中找到最接近 df2 中的点,并计算它们之间的距离,以 km 为单位并将每个值附加到 df2 中的新列。

我想出了一个解决方案,但请记住 df+700,000 行而 df2 有大约 60,000 行,所以我的解决方案将花费太长时间来计算。我能想到的唯一解决方案是使用双 for 循环...

def compute_shortest_dist(df, df2):
    # array to store all closest distances
    shortest_dist = []

    # radius of earth (used for calculation)
    R = 6373.0
    for i in df2.index:
        # keeps track of current minimum distance
        min_dist = -1

        # latitude and longitude from df2
        lat1 = df2.ix[i]['Latitude']
        lon1 = df2.ix[i]['Longitude']

        for j in df.index:

            # the following is just the calculation necessary
            # to calculate the distance between each point in km
            lat2 = df.ix[j]['Latitude']
            lon2 = df.ix[j]['Longitude']
            dlon = lon2 - lon1
            dlat = lat2 - lat1
            a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
            c = 2 * atan2(sqrt(a), sqrt(1 - a))
            distance = R * c

            # store new shortest distance
            if min_dist == -1 or distance > min_dist:
                min_dist = distance
        # append shortest distance to array
        shortest_dist.append(min_dist)

此函数的计算时间太长,我知道必须有更有效的方法,但我不太擅长 pandas 语法。

感谢任何帮助。

最佳答案

您可以在 numpy 中编写内部循环,这应该会显着加快速度:

import numpy as np

def compute_shortest_dist(df, df2):
    # array to store all closest distances
    shortest_dist = []

    # radius of earth (used for calculation)
    R = 6373.0
    lat1 = df['Latitude']
    lon1 = df['Longitude']
    for i in df2.index:
        # the following is just the calculation necessary
        # to calculate the distance between each point in km
        lat2 = df2.loc[i, 'Latitude']
        dlat = lat1 - lat2
        dlon = lon1 - df2.loc[i, 'Longitude']
        a = np.sin(dlat / 2)**2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon / 2)**2
        distance = 2* R * np.arctan2(np.sqrt(a), np.sqrt(1 - a))

        # append shortest distance to array
        shortest_dist.append(distance.min())
    return shortest_dist

关于python - 列之间的计算(经度/纬度)非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48701852/

相关文章:

python - 如何按星期几和一天中的小时过滤 Pandas DatetimeIndex

python - 如何将物体随机放置在不同角度和不同位置的python上

python - Django 中的 TypedChoiceField 或 ChoiceField

python - 根据 Pandas 中的整数分解行

python - pandas 内存消耗 hdf 文件分组

c++ - 为锦标赛系统分配奖品

python - 在python中将一个16位整数拆分为两个8位整数

algorithm - 如何在整数范围内找到不同的数字集?

python - 如何修复打印语句,使其在一行而不是两行?

python - 捕获 PyQt 异常