Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write multiple NumPy arrays into CSV file in separate columns?

Tags:

python

csv

numpy

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!

like image 602
oops Avatar asked Jul 26 '12 02:07

oops


People also ask

How do I save multiple NumPy arrays to a file?

Save several arrays into a single file in uncompressed . npz format. Provide arrays as keyword arguments to store them under the corresponding name in the output file: savez(fn, x=x, y=y) . If arrays are specified as positional arguments, i.e., savez(fn, x, y) , their names will be arr_0, arr_1, etc.

How do I convert a NumPy array to a csv file?

You can save your NumPy arrays to CSV files using the savetxt() function. This function takes a filename and array as arguments and saves the array into CSV format. You must also specify the delimiter; this is the character used to separate each variable in the file, most commonly a comma.

How do I append multiple NumPy arrays?

Use numpy. concatenate() to merge the content of two or multiple arrays into a single array. This function takes several arguments along with the NumPy arrays to concatenate and returns a Numpy array ndarray. Note that this method also takes axis as another argument, when not specified it defaults to 0.

How do I save an array of numbers in CSV format?

savetext() This method is used to save an array to a text file. Create an array then save it as a CSV file.


1 Answers

I think this should do what you want:

output = np.column_stack((arrA.flatten(),arrB.flatten(),arrC.flatten()))
np.savetxt('output.dat',output,delimiter=',')
like image 92
user545424 Avatar answered Oct 12 '22 12:10

user545424