我有两个数据框。
df1 具有 580 条唯一记录 - 包含纬度和经度信息
df2 具有 490000 条唯一记录 - 包含纬度和经度信息
我正在尝试从这 580 个位置中获取有多少个位置在距离 490000 个位置的 400 米半径内。
我正在使用以下代码,它可以工作。
from __future__ import print_function
from config import conn
from pandas import DataFrame
import pandas as pd
import math
def distance(origin, destination):
lat1, lon1 = origin
lat2, lon2 = destination
radius = 6371 *1000# km
dlat = math.radians(lat2-lat1)
dlon = math.radians(lon2-lon1)
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
* math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = radius * c
return d
def convertTuple(tup):
str = ''.join(tup)
return str
df1 = pd.read_csv("/home/ubuntu/maid80.csv")
df2 = pd.read_csv("/home/ubuntu/iodr.csv")
ll = []
for index,rows in df2.iterrows():
lat1 = rows['latitude']
lon1 = rows['longitude']
for i,r in df1.iterrows():
k = distance((lat1,lon1),(r['latitude'],r['longitude']))
if (k <= 400):
ll.append(rows['id'])
# print(ll)
print(index)
myset = set(ll)
print(myset)
我正在我的笔记本电脑上运行这个程序,需要超过两个小时才能完成所有580次迭代。我担心第二个数据集中的记录数量会增加。
有更好的方法可以节省时间吗?