1

I have a pandas dataFrame like this.

enter image description here

The X, Y, Z are they (x,y,z) coordinates that represent a point inside a cube of side-length 255.

I want to create a numpy array/dataFrame from this, whose index will be the (x,y,z) coordinates and value is the intensity.

the output should be

data[133,55,250] = 8
data[133,61,254] = 21
...

I tried something like this

data = np.zeros((255,255,255), dtype=np.int)
index = np.array((temp['X'], temp['Y'], temp['Z']))

but returned index is a (3,15) array.

I want a index where

data[index] = intensity

will give me my result.

1 Answer 1

1

Instead of

index = np.array((temp['X'], temp['Y'], temp['Z']))

you could use integer array indexing to perform the assignment:

data = np.zeros((256, 256, 256), dtype=np.int)
data[temp['X'], temp['Y'], temp['Z']] = temp['intensity']
Sign up to request clarification or add additional context in comments.

3 Comments

Are you using data[temp['X'], temp['Y'], temp['Z']] = temp['intensity']? My first answer was incorrect.
if I do like that, I get this error IndexError Traceback (most recent call last) <ipython-input-46-aec532c66c2e> in <module>() 1 data = np.zeros((255,255,255), dtype=np.int) ----> 2 data[temp['X'], temp['Y'], temp['Z']] = temp['Intensity'] IndexError: index 255 is out of bounds for axis 2 with size 255
That means you have a value of 255 in temp[['X','Y','Z']], but data = np.zeros((255,255,255)) is only indexable up to 254 since Python uses 0-based indexing. You can fix it by defining data = np.zeros((256,256,256)).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.