How to save stack of arrays to .csv when there is a mismatch between array dtype and format specifier?
<p>These are my stacks of arrays, both with variables arranged columnwise.</p>

<pre><code>final_a = np.stack((four, five, st, dist, ru), axis=-1)

final_b = np.stack((org, own, origin, init), axis=-1)
</code></pre>

<p>Example:</p>

<pre><code>In: final_a 
Out: array([['9999', '10793', ' 1', '99', '2'],
       ['9999', '10799', ' 1', '99', '2'],
       ['9999', '10712', ' 1', '99', '2'],
       ..., 
       ['9999', '23960', '33', '99', '1'],
       ['9999', '82920', '33', '99', '2'],
       ['9999', '82920', '33', '99', '2']], 
      dtype='&lt;U5')
</code></pre>

<p>But when I try to save either of them to a .csv file using this code:</p>

<pre><code>np.savetxt("/Users/jaisaranc/Documents/ASI selected data - A.csv", final_a, delimiter=",")
</code></pre>

<p>It throws this error:</p>

<pre><code>TypeError: Mismatch between array dtype ('&lt;U5') and format specifier ('%.18e,%.18e,%.18e,%.18e,%.18e')
</code></pre>

<p>I have no idea what to do.</p>

<p><code>savetxt</code> in Numpy allows you <a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html" rel="nofollow">specify a format</a> for how the array will be displayed when it's written to a file. The default format (<code>fmt='%.18e'</code>) can only format arrays containing only numeric elements. Your array contains strings (<code>dtype='&lt;U5'</code> means the type is unicode with length 5) so it raises an error. In your case you should also include <code>fmt='%s'</code> as an argument to ensure the array elements in the output file are formatted as strings. For example:</p>

<pre><code>np.savetxt("example.csv", final_a, delimeter=",", fmt="%s")  
</code></pre>

python arrays python-3.x csv