我有一个像这样的 pandas dataFrame。
X、Y、Z 是 (x,y,z) 坐标,表示边长为 255 的立方体内的点。
我想从中创建一个 numpy 数组/数据帧,其索引是 (x,y,z) 坐标,值是强度。
输出应该是
data[133,55,250] = 8
data[133,61,254] = 21
...
我尝试过这样的事情
data = np.zeros((255,255,255), dtype=np.int)
index = np.array((temp['X'], temp['Y'], temp['Z']))
但返回的索引是一个(3,15)数组。
我想要一个索引,其中
data[index] = intensity
会给我结果。
最佳答案
而不是
index = np.array((temp['X'], temp['Y'], temp['Z']))
你可以使用integer array indexing执行任务:
data = np.zeros((256, 256, 256), dtype=np.int)
data[temp['X'], temp['Y'], temp['Z']] = temp['intensity']
关于python - 如何使用 pandas DataFrame 的值作为 numpy 数组索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27103073/