2014-11-24 67 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

相關問題