Write multiple Numpy arrays of different dtype as columns of CSV file
<p>What would be the best way to write multiple numpy arrays of different dtype as different columns of a single CSV file?</p>

<p>For instance, given the following arrays:</p>

<pre><code>array([[1, 2],
       [3, 4],
       [5, 6]])

array([[ 10.,  20.],
       [ 30.,  40.],
       [ 50.,  60.]])
</code></pre>

<p>I would like to obtain a file (delimiter irrelevant):</p>

<pre><code>1 2 10.0 20.0
3 4 30.0 40.0
5 6 50.0 60.0
</code></pre>

<p>Optimally, I would like to be able to write a list of arrays this way, where a format/dtype can be different for every array.</p>

<p>I tried looking at <code>savetxt</code>, but it's not clear to me how to use it if the arrays have a different type.</p>

<p>use <code>np.concatenate</code> in order to concatenate the arrays along the second axis then use <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html#numpy.savetxt" rel="nofollow"><code>np.savetxt</code></a> inorder to save your array in a a text format.</p>

<pre><code>import numpy as np

a = np.array([[1, 2],
              [3, 4],
              [5, 6]])

b = np. array([[10., 20.],
               [30., 40.],
               [50., 60.]])

np.savetxt('filename.csv', np.concatenate((a,b), axis=1))
</code></pre>

<p>Note that <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html#numpy.savetxt" rel="nofollow"><code>np.savetxt</code></a> also accepts another arguments like <code>delimiter</code>.</p>

<blockquote>
  <p><code>numpy.savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', footer='', comments='# ')</code></p>
</blockquote>

python csv numpy