9

I have the follwoing problem. I have a 3D array like matrix = np.zeros((30,30,100)) where every entry is a coordinate and gets a value. So matrix [0][0][0] is the coordinate x=0,y0,z=0 and has a value of 0. Now i want to store all the values in a csv like this where the first 3 rows are the coordinates and the 4th the corresponding value:

enter image description here

Is there a fast way with numpy to do this?

4
  • where does your corresponding value come from? Oct 20, 2017 at 15:58
  • They are stored in another process, but for this example it's also ok to store only zeros.
    – Varlor
    Oct 20, 2017 at 16:00
  • Does it have to be a csv file? I have found that np.save is more useful for saving ND arrays in numpy.
    – BenT
    Oct 20, 2017 at 16:04
  • I think numpy.savetxt do the same. As long it has the structure like above, it's ok
    – Varlor
    Oct 20, 2017 at 16:07

2 Answers 2

9

You could use pandas, it can both reshape the array and save it as csv.

import numpy as np
import pandas as pd
# create an example array
a = np.arange(24).reshape([2,3,4])
# convert it to stacked format using Pandas
stacked = pd.Panel(a.swapaxes(1,2)).to_frame().stack().reset_index()
stacked.columns = ['x', 'y', 'z', 'value']
# save to disk
stacked.to_csv('stacked.csv', index=False)

Otherwise, you can apply

np.ravel()

to your array and then restore indices using one of the recipes in this question.

1
  • 1
    Panel is outdated.
    – Rashik
    Jun 21, 2020 at 19:36
1

I imagine you get the coordinates with the indices:

def iter_3D(matrix):
    for i in range(matrix.shape[0]):
        for j in range(matrix.shape[1]):
            for k in range(matrix.shape[2]):
                yield i, j, k

l = []

for i, j, k in iter_3D(matrix):
    l.append('%d %d %d %d' %(str(indices_x(i, j, k)), str(indices_y(i, j, k)), str(indices_z(i, j, k)), str(matrix[i, j, k]))

with open('file.csv', 'w') as f:
    f.write("\n".join(l))

More sophisticated solutions are possible, but this should be the core. Have a look at: csv io in the python docs or nditer if you want a more sophisticated iteration method or use pandas (takes a little time to get the hang out of it).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.