加速熊猫iterrows(xy到lat长坐标pyproj)

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

我一直在使用iterrows使用pyProj模块将XY坐标转换为Lat,Long。我知道在熊猫中使用iterrows很慢,但我无法找到另一种方法来编写代码。

我有一个带有井号的数据框,每个井有X和Y坐标。我还有一个带有ESPG坐标系的列,可以通过pyProj读取。这种EPSG坐标系对于许多不同的井而言是不同的。我提供了一个示例数据帧。

data = pd.DataFrame({"WellName": ("well1","well2","well3","well4","well5"),"EPSG": ('epsg:21898','epsg:21898','epsg:21897','epsg:21897','epsg:21897'),'X':(900011,900011,900011,900011,900011),'Y':(800011,800011,800011,800011,800011)})
data

我循环遍历此数据帧的每一行,找到epsg坐标系,然后将x,y转换为lat,long。这有效,但速度极慢。是否有一个更简单,更优雅的解决方案,可以加快它?

import pandas as pd
import numpy as np
from pyproj import Proj, transform


for index, row in data.iterrows():
        # epsg coord system (from EPSG row)
        inProj = Proj(init=row['EPSG'])
        # espg coord system for lat long
        outProj = Proj(init='epsg:4326')
        # X and Y coords (from X and Y rows)
        x1,y1 = row['X'],row['Y']#output
        x2,y2 = transform(inProj,outProj,x1,y1)
        #print (x2,y2)
        # create and fill in lat and long columns
        data.loc[index,'latitude'] = x2
        data.loc[index,'longitude'] = y2
        #print (row['name'],row['X'],(row['EPSG']))

我试图对它进行矢量化,但我不知道我在做什么,它会让我的python崩溃。我不建议使用它...:/

data['latitude'],data['longitude'] = transform(Proj(init=(data['EPSG'])), Proj(init='epsg:4326'), data['X'], data['Y'])

中途解决方案:

经过多次尝试,我已经部分解决了我的问题。使用“apply”现在速度提高了几个数量级

它创建了一个带有lat,long的新元组列。然后,我必须执行一些解决方案,为元组创建两个单独的列(一个用于lat,一个用于long)。

    data['LatLong'] = data.apply(lambda row:  transform(Proj(init=row['EPSG']),Proj(init='epsg:4326'),row['X'],row['Y']), axis=1)

LatLongIndex = pd.DataFrame(data['LatLong'].values.tolist(), index=data.index)
dfDevLatLong = pd.merge(dataSDX,LatLongIndex, right_index=True, left_index=True)
dfDevLatLong

它现在可行,但仍然有点慢,我相信有更优雅的方式来解决这个问题。

python pandas vectorization pyproj
1个回答
0
投票

我已经部分解决了我的问题。使用“apply”现在速度提高了几个数量级

它创建了一个带有lat,long的新元组列。然后,我必须执行一些解决方案,为元组创建两个单独的列(一个用于lat,一个用于long)。

    data['LatLong'] = data.apply(lambda row:  transform(Proj(init=row['EPSG']),Proj(init='epsg:4326'),row['X'],row['Y']), axis=1)

LatLongIndex = pd.DataFrame(data['LatLong'].values.tolist(), index=data.index)
dfDevLatLong = pd.merge(dataSDX,LatLongIndex, right_index=True, left_index=True)
dfDevLatLong

它现在可行,但仍然有点慢,我相信有更优雅的方式来解决这个问题。

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