2014-11-24 68 views
1

我有一个像这样的熊猫数据框。如何使用熊猫数据框的值作为numpy数组索引

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

而不是

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

一个问题。如果我在多维数据集中缺少一些数据会怎么样? 我只有1000点的强度值信息。它给出了一个值错误。 形状不匹配:形状的值阵列(1000)不能被广播到形状的索引结果(1000,3,255,255) 我的原始数据是1000行×5列 – Sounak 2014-11-24 11:33:00

+0

是否使用'数据[临时[“X” ],temp ['Y'],temp ['Z']] = temp ['intensity']'?我的第一个回答是错误的。 – unutbu 2014-11-24 11:34:03

+0

如果我喜欢的是,我得到这个错误 IndexError回溯(最近最后调用) () 1数据= np.zeros((255,255,255),D型= NP .int) ----> 2数据[temp ['X'],temp ['Y'],temp ['Z']] = temp ['Intensity'] IndexError:index 255超出范围对于大小为255的轴2 – Sounak 2014-11-24 11:40:00

相关问题