Surfcast23 Surfcast23 - 3 months ago 23
Python Question

Saving arrays as columns with np.savetxt

I am trying to do something that is probable very simple. I would like to save three arrays to a file as columns using 'np.savetxt' When I try this

x = [1,2,3,4]
y = [5,6,7,8]
z = [9,10,11,12]

np.savetxt('myfile.txt', (x,y,z), fmt='%.18g', delimiter=' ', newline=os.linesep)


The arrays are saved like this

1 2 3 4
5 6 7 8
9 10 11 12


But what I wold like is this

1 5 9
2 6 10
3 7 11
4 8 12


Thanks in advance

Answer

Use numpy.c_[]:

np.savetxt('myfile.txt', np.c_[x,y,z])
Comments