user1821176
user1821176

Reputation: 1191

Column stacking multidemensional array

Suppose I have a multidimensional array

a = np.array([(1,2,3,4), (11,21,31,41), (3,3,3,3), (12, 24, 15, 100)])

I am wondering if there is a way to use numpy column stack such that when I output it to a another file using numpy.savetxt I get what I display below?

1 11 3 12
2 21 3 24
3 31 3 15
4 41 3 100

Upvotes: 1

Views: 62

Answers (1)

Daniel
Daniel

Reputation: 19547

Simply save the array as the transpose:

>>> np.savetxt('dat',a.T)
>>> np.loadtxt('dat')
array([[   1.,   11.,    3.,   12.],
       [   2.,   21.,    3.,   24.],
       [   3.,   31.,    3.,   15.],
       [   4.,   41.,    3.,  100.]])

Or if you do not want decimals in the saved text:

>>> np.savetxt('dat',a.T,fmt='%.0f')

####@glados:$ head dat
1 11 3 12
2 21 3 24
3 31 3 15
4 41 3 100

Upvotes: 1

Related Questions