savetxt two columns in python,numpy
Question: I have some data as numpy 2D array list-

    
    
    array([[ 0.62367947],
           [ 0.95427859],
           [ 0.97984112],
           [ 0.7025228 ],
           [ 0.86436385],
           [ 0.71010739],
           [ 0.98748138],
           [ 0.75198057]])
    
    array([[-1.,  1., -1.],    
           [-1.,  1.,  1.],
           [ 1.,  1.,  1.],
           [ 1., -1.,  1.],
           [-1., -1., -1.],
           [ 1.,  1., -1.],
           [ 1., -1., -1.],
           [-1., -1.,  1.]])
    

And I want to save them in a txt file so that they look like

    
    
    0.62367947    -1 1 -1    
    0.95427859    -1 1 1    
    0.97984112     1 1 1
    

Can someone help me how I can do it using numpy savetxt


Answer: 
    import numpy as np
    R = np.array([[0.62367947],
                  [0.95427859],
                  [0.97984112],
                  [0.7025228],
                  [0.86436385],
                  [0.71010739],
                  [0.98748138],
                  [0.75198057]])
    
    phase = np.array([[-1., 1., -1.],
                      [-1., 1., 1.],
                      [1., 1., 1.],
                      [1., -1., 1.],
                      [-1., -1., -1.],
                      [1., 1., -1.],
                      [1., -1., -1.],
                      [-1., -1., 1.]])
    
    np.savetxt('R2.txt', np.hstack([R, phase]), fmt=['%0.8f','%g','%g','%g'])    
    

yields

    
    
    0.62367947 -1 1 -1
    0.95427859 -1 1 1
    0.97984112 1 1 1
    0.70252280 1 -1 1
    0.86436385 -1 -1 -1
    0.71010739 1 1 -1
    0.98748138 1 -1 -1
    0.75198057 -1 -1 1
    

* * *

[np.hstack](http://docs.scipy.org/doc/numpy/reference/generated/numpy.hstack.html)
stacks arrays horizontally. Since `R` and `phase` are both 2-dimensional,
`np.hstack([R, phase])` yields

    
    
    In [137]: np.hstack([R,phase])
    Out[137]: 
    array([[ 0.62367947, -1.        ,  1.        , -1.        ],
           [ 0.95427859, -1.        ,  1.        ,  1.        ],
           [ 0.97984112,  1.        ,  1.        ,  1.        ],
           [ 0.7025228 ,  1.        , -1.        ,  1.        ],
           [ 0.86436385, -1.        , -1.        , -1.        ],
           [ 0.71010739,  1.        ,  1.        , -1.        ],
           [ 0.98748138,  1.        , -1.        , -1.        ],
           [ 0.75198057, -1.        , -1.        ,  1.        ]])
    

Passing this 2D array to `np.savetxt` gives you the desired result.

