【发布时间】:2020-11-15 18:30:22
【问题描述】:
我不确定如何最好地解决这个问题,但我有一个数据文件,其中包含坐标 x、y 和一些量级的列表。让我们说人口。
X, Y, POP
1.2, 1.3, 1000
22.5, 2.5, 250
...
98.6, 1.7, 1500
首先,我将 X 和 Y 舍入到最接近的 int 并根据最小值和最大值的范围创建网格。
Xmin = np.amin(df['X'])
Xmax = np.amax(df['X'])
Ymin = np.amin(df['Y'])
Ymax = np.amax(df['Y'])
## I use a step of 10 as I don't have enough memory for a step counter of 1
## This is where the problem is
X = np.arange(int(Xmin), int(Xmax), 10)
Y = np.arange(int(Ymin), int(Ymax), 10)
xx, yy = np.meshgrid(X, Y)
所以现在我有一个网格网格,其中包含我所有的坐标。现在的问题在于,我希望这个新的网格包含来自数据帧的幅度值。
因此我想将我的 df 映射到网格网格上,给我这样的东西。
X, Y, POP
1, 1, 1000
10, 1, N/A
20, 1, 250
...
90, 1, 1500
这是我用来解决问题的方法,但是速度很慢,并且会比较每个值。我想我的问题归结为,与下面的代码相比,是否有更快/更有效的方法?最终,我计划使用周围单元格的平均值来填充 N/A 值。但要做到这一点,我想首先将所有内容映射到一个漂亮的统一网格。
state = np.empty([X.shape[0], Y.shape[0]])
state[:] = np.nan
for i in range(0, len(X), 1):
for j in range(0, len(Y), 1):
for z in range(0, len(df['X']), 1):
if (abs(X[i] - df['X'][z]) < 10) and (abs(Y[j] - df['Y'][z]) < 10):
state[i,j] = df['POP'][z]
【问题讨论】:
-
您应该考虑对两个数组进行 pandas 连接。
标签: python python-3.x numpy