Ang*_*elo 1 python numpy scipy python-3.x pandas
我有一个包含x,y坐标列表的pandas数据帧,我正在使用scipy.spatial在给定额外点的情况下找到数据帧中的最近点.
import pandas as pd
import numpy as np
import scipy.spatial as spatial
stops = pd.read_csv("stops.csv")
pt = x,y
points = np.array(zip(stops['stop_lat'],stops['stop_lon']))
nn = points[spatial.KDTree(points).query(pt)[1]]
Run Code Online (Sandbox Code Playgroud)
现在,在python 2.7中,这个工作完美无缺.在python 3.5中,我收到以下错误:
.../scipy/spatial/kdtree.py", line 231, in __init__
self.n, self.m = np.shape(self.data)
ValueError: not enough values to unpack (expected 2, got 0)
Run Code Online (Sandbox Code Playgroud)
在文档中我找不到任何有用的东西.
在Python3中,zip()
返回迭代器对象而不是元组列表.points
因此将是np.object
包含zip
迭代器的0维数组,而不是x,y坐标的2D数组.
你可以list
从迭代器构造一个:
points = np.array(list(zip(stops['stop_lat'],stops['stop_lon'])))
Run Code Online (Sandbox Code Playgroud)
但是,更优雅的解决方案可能是zip
通过索引数据帧的多个列来避免完全使用:
points = stops[['stop_lat','stop_lon']].values
Run Code Online (Sandbox Code Playgroud)