6

How to write multiple numpy arrays into one csv file in multiple columns?

import numpy
import csv

arrA = numpy.array(file.root.a)
arrB = numpy.array(file.root.b)
arrC = numpy.array(file.root.c)

for i in range (480):
    for j in range (640):
        (write arrA[i,j] into column1,write arrB[i,j] into column2,write arrC[i,j] into column3)

Thanks a lot!

1 Answer 1

11

I think this should do what you want:

output = np.column_stack((arrA.flatten(),arrB.flatten(),arrC.flatten()))
np.savetxt('output.dat',output,delimiter=',')
Sign up to request clarification or add additional context in comments.

4 Comments

I got:File "/usr/lib64/python2.4/site-packages/numpy/lib/io.py", line 542, in savetxt fh.write(format % tuple(row) + '\n') TypeError: float argument required
What are the sizes and data types of arrA, arrB, and arrC?
What are the shapes of the arrays; i.e. what is arrA.shape?
@oops: updated my answer. np.dstack was adding an extra dimension to updated.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.