【问题标题】:Numpy array with coordinates带有坐标的numpy数组
【发布时间】:2018-02-13 00:50:45
【问题描述】:

我有一个 DataFrame,其中有一列坐标不同,在其他列表中聚集在一起,如下所示:

    name    OBJECTID    geometry
0    NaN           1    ['-80.304852,-3.489302,0.0','-80.303087,-3.490214,0.0',...]

1    NaN           2    ['-80.27494,-3.496571,0.0',...]

2    NaN           3    ['-80.267987,-3.500003,0.0',...]

我想分隔值并删除“0.0”,但将它们保留在列表中以将它们添加到字典中的某个键,如下所示:

    name    OBJECTID    geometry
0    NaN           1    [[-80.304852, -3.489302],[-80.303087, -3.490214],...]

1    NaN           2    [[-80.27494, -3.496571],...]

2    NaN           3    [[-80.267987, -3.500003],...]

这是我的代码在我尝试在 for 循环中分隔它们时不起作用:

import panda as pd
import numpy as np

r = pd.read_csv('data.csv') 
rloc = np.asarray(r['geometry'])

r['latitude'] = np.zeros(r.shape[0],dtype= r['geometry'].dtype)
r['longitude'] = np.zeros(r.shape[0],dtype= r['geometry'].dtype)

# Separating the latitude and longitude values form each string.
for i in range(0, len(rloc)):
    for j in range(0, len(rloc[i])):
        coord = rloc[i][j].split(',')
        r['longitude'] = coord[0]
        r['latitude'] = coord[1]

r = r[['OBJECTID', 'latitude', 'longitude', 'name']]

编辑:结果不好,因为它只为每个值打印一个值。

  OBJECTID  latitude    longitude   name
0        1  -3.465566   -80.151633  NaN
1        2  -3.465566   -80.151633  NaN
2        3  -3.465566   -80.151633  NaN

额外问题:如何在一个元组中添加所有这些经度和纬度值以与 geopy 一起使用?像这样:

r['location'] = (r['latitude], r['longitude'])

因此,几何列将如下所示:

geometry
[(-80.304852, -3.489302),(-80.303087, -3.490214),...]

[(-80.27494, -3.496571),...]

[(-80.267987, -3.500003),...]

编辑:

数据起初看起来像这样(对于每一行):

<LineString><coordinates>-80.304852,-3.489302,0.0 -80.303087,-3.490214,0.0 ...</coordinates></LineString>

我用正则表达式修改了它,使用以下代码:

geo = np.asarray(r['geometry']); 
geo = [re.sub(re.compile('<.*?>'), '', string) for string in geo]

然后我把它放在一个数组中:

rv = [geo[i].split() for i in range(0,len(geo))]
r['geometry'] = np.asarray(rv)

当我调用 r['geometry'] 时,输出为:

0    [-80.304852,-3.489302,0.0, -80.303087,-3.49021...
1    [-80.27494,-3.496571,0.0, -80.271963,-3.49266,...
2    [-80.267987,-3.500003,0.0, -80.267845,-3.49789...
Name: geometry, dtype: object

r['geometry'][0] 是:

 ['-80.304852,-3.489302,0.0',
 '-80.303087,-3.490214,0.0',
 '-80.302131,-3.491878,0.0',
 '-80.300763,-3.49213,0.0']

【问题讨论】:

  • 你得到了什么结果?
  • 更新结果!它不起作用,因为里面的列表已被删除...我正在尝试找到解决方法。

标签: python pandas numpy


【解决方案1】:

使用玩具数据集输入的 pandas 解决方案:

df = pd.read_csv("test.txt")
   name  OBJECTID                                           geometry
0   NaN         1  ['-80.3,-3.4,0.0','-80.3,-3.9,0.0','-80.3,-3.9...
1   NaN         2  ['80.2,-4.4,0.0','-81.3,2.9,0.0','-80.7,-3.2,0...
2   NaN         3  ['-80.1,-3.2,0.0','-80.8,-2.9,0.0','-80.1,-1.9...

现在转换成经纬度对的列:

#regex extraction of longitude latitude pairs
pairs = "(-?\d+.\d+,-?\d+.\d+)"
s = df["geometry"].str.extractall(pairs)
#splitting string into two parts, creating two columns for longitude latitude
s = s[0].str.split(",", expand = True)  
#converting strings into float numbers - is this even necessary?
s[[0, 1]] = s[[0, 1]].apply(pd.to_numeric)
#creating a tuple from longitude/latitude columns
s["lat_long"] = list(zip(s[0], s[1]))
#placing the tuples as columns in original dataframe 
df = pd.concat([df, s["lat_long"].unstack(level = -1)], axis = 1)

玩具数据集的输出:

   name  OBJECTID                                           geometry  \
0   NaN         1  ['-80.3,-3.4,0.0','-80.3,-3.9,0.0','-80.3,-3.9...   
1   NaN         2  ['80.2,-4.4,0.0','-81.3,2.9,0.0','-80.7,-3.2,0...   
2   NaN         3  ['-80.1,-3.2,0.0','-80.8,-2.9,0.0','-80.1,-1.9...   

               0              1              2  
0  (-80.3, -3.4)  (-80.3, -3.9)  (-80.3, -3.9)  
1   (80.2, -4.4)   (-81.3, 2.9)  (-80.7, -3.2)  
2  (-80.1, -3.2)  (-80.8, -2.9)  (-80.1, -1.9)  

或者,您可以将一列中的元组组合为一个列表:

s["lat_long"] = list(zip(s[0], s[1]))
#placing the tuples as a list into a column of the original dataframe 
df["lat_long"] = s.groupby(level=[0])["lat_long"].apply(list)

现在输出:

   name  OBJECTID                                           geometry  \
0   NaN         1  ['-80.3,-3.4,0.0','-80.3,-3.9,0.0','-80.3,-3.9...   
1   NaN         2  ['80.2,-4.4,0.0','-81.3,2.9,0.0','-80.7,-3.2,0...   
2   NaN         3  ['-80.1,-3.2,0.0','-80.8,-2.9,0.0','-80.1,-1.9...   

                                        lat_long  
0  [(-80.3, -3.4), (-80.3, -3.9), (-80.3, -3.9)]  
1    [(80.2, -4.4), (-81.3, 2.9), (-80.7, -3.2)]  
2  [(-80.1, -3.2), (-80.8, -2.9), (-80.1, -1.9)]  

【讨论】:

  • 列表 's' 似乎是空的。 's = df["geometry"].str.extractall(pairs)' 行没有做任何事情,如果我尝试将 s 打印出来,我只会得到一个以 0 作为列名的空数据框。
  • 您始终应该提供Minimal, Complete and Verifiable example。我使用了此处给出的信息,但似乎场几何与您的描述不同。您能否上传一个示例文件并描述您如何创建数据框,以便我调整脚本?
  • 使用新示例更新帖子。
  • 是否有可能df['geometry'].str.extractall(pairs) 不起作用,因为 dtype 是“对象”?
  • 我更新了代码,它现在也可以在不等长的情况下使用。解决方案来自Wen, please give him an upvote for his contribution
【解决方案2】:

在您的代码中,您有效地将上次迭代的经度和纬度值分配给完整的列。您也可以将字符串转换为浮点数:

# Separating the latitude and longitude values form each string.
for i in range(0, len(rloc)):
    r['longitude'][i] = []
    r['latitude'][i] = []
    for j in range(0, len(rloc[i])):
        coord = rloc[i][j].split(',')
        r['longitude'][i].append(float(coord[0]))
        r['latitude'][i].append(float(coord[1]))

争取奖金:)

for i in range(0, len(rloc)):
    r['geometry'][i] = [
        (
            float(element.split(',')[0]),
            float(element.split(',')[1])
        ) for element in r['geometry'][i]
    ]

【讨论】:

  • 谢谢!这行得通!我选择了另一个作为答案,因为它的计算效率要高得多,因为我有超过一百万个条目要处理。
猜你喜欢
  • 2019-12-02
  • 1970-01-01
  • 2011-07-26
  • 2015-10-26
  • 2019-10-21
  • 2012-04-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多