Apply函数包含pandas中的外部库:如何使其更快?

问题描述 投票:0回答:1

我有数据框,其中行数约为900万行,包含纬度和经度,如下所示:

enter image description here

我试图通过使用OSMnx库获取最近的节点和每个点到最近节点的距离,通过应用以下代码:

def nearest_node(Lat,Lon):
    nearest_node,dist=ox.get_nearest_node(G, (Lat,Lon), return_dist=True)  
    return nearest_node 
def dist_to_Nnode(Lat,Lon):
    nearest_node,dist=ox.get_nearest_node(G, (Lat,Lon), return_dist=True)
    return dist 


df['nearest_node'] = np.vectorize(nearest_node)(df['Lat'],df['Lon'])

df['dist_to_Nnode'] = np.vectorize(dist_to_Nnode)(df['Lat'],df['Lon'])

其中G是网络图,由以下Codeline获得:

import osmnx as ox 
import networkx as nx
import os 


os.environ["PROJ_LIB"] =r'C:\Users\****\Anaconda3\Library\share'
import osmnx as ox
Graph_x= ox.graph_from_place('Beijing, China',  which_result=2)
G= ox.project_graph(Graph_x,to_crs={'proj':'longlat','epsg':'32750' ,'ellps':'WGS84', 'datum':'WGS84'}) #wgs 84 50S

我将之前的代码应用于df的样本,它已经给出了所需的结果,但它花了很多时间用于总的df。如何更快地运行此代码?

pandas numpy pyspark time-complexity vectorization
1个回答
1
投票

根据OSMnx documentation,使用ox.get_nearest_nodes(G, X, Y, method='kdtree'),其中G是投影图,X和Y是投影x和y坐标的矢量。或者,如果您必须完全使用未投影的lat-lng,则使用method='balltree'

© www.soinside.com 2019 - 2024. All rights reserved.