I need to insert a Value(Float) at XYZ(int) from CSV file into a world = numpy.zeros((360,360,360))

Example of rows in CSV file:

Value   X   Y   Z
4.5396  159 159 1
4.5905  182 160 3
etc

I have about 16000 rows in the CSV file, though many are duplicates.

My main problem is that i need to use Integers(XYZ) to insert a Float(Value) into Numpy where it refuse to handle both at the same time and i have also tried with Pandas but same problem.

Here i have converted it all to integers and is the only way i have made it work-ish.

world = numpy.zeros((360,360,360))
data = numpy.genfromtxt('Myfile.csv', delimiter=',',skip_header=1) 
for Value,X, Y, Z in data.astype(int):
         world[X, Y, Z] = Value

It should look like this

world[159,159,1] = 4.5369
world[182,160,3] = 4.5945 

but i can only get this

world[159,159,1] = 4
world[182,160,3] = 4

everything else results in various errors

1 Answers

0
jdehesa On Best Solutions

Do this instead:

world = numpy.zeros((360,360,360))
data = numpy.genfromtxt('Myfile.csv', delimiter=',', skip_header=1) 
X, Y, Z = data[:, 1:].astype(int).T
world[X, Y, Z] = data[:, 0]

Or to read the text data directly with the correct type:

world = numpy.zeros((360,360,360))
data = numpy.genfromtxt('Myfile.csv', delimiter=',', skip_header=1,
                        dtype=[('Value', float), ('X', int), ('Y', int), ('Z', int)]) 
world[data['X'], data['Y'], data['Z']] = data['Value']