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!
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.
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.
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.
savetext() This method is used to save an array to a text file. Create an array then save it as a CSV file.
I think this should do what you want:
output = np.column_stack((arrA.flatten(),arrB.flatten(),arrC.flatten()))
np.savetxt('output.dat',output,delimiter=',')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With