如何使用pandas DataFrame的值作为numpy数组索引

时间:2014-11-24 10:51:03

标签: python arrays numpy pandas dataframe

我有像这样的pandas dataFrame。

enter image description here

X,Y,Z是它们(x,y,z)坐标,表示边长255的立方体内的一个点。

我想从中创建一个numpy数组/ dataFrame,其索引将是(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

会给我我的结果。

我有点迷茫。一些帮助将不胜感激。

感谢。

1 个答案:

答案 0 :(得分:1)

而不是

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']