numpy.savetxt- Save one column as int and the rest as floats?
<p><strong>The Problem</strong></p>

<p>So I have a 2D array (151 rows, 52 columns) I'd like to save as a text file using np.savetxt. However, I want the first column's numbers to save as integers (1950, 1951, etc) while the rest of the data saves as precision 5 (4 if rounded) floating point numbers (2.7419, 2.736, etc). I can't figure out how to do this.</p>

<p><strong>The Code</strong></p>

<p>When I print the first 4 rows &amp; 3 columns of the output of the array, it looks like this.</p>

<p>[[ 1950.         2.7407     2.7396]</p>

<p>[ 1951.          2.7419     2.736 ]</p>

<p>[ 1952.          2.741      2.7374]</p>

<p>[ 1953.          2.7417     2.7325]]</p>

<p>When I use the following...</p>

<pre><code>np.savetxt('array.txt',data,fmt="%1.4f")
</code></pre>

<p>The array saves the first column as a precision 5 floating point numbers like the rest of the data (1950.0000, 1951.0000, etc). When I try to specify different formats as such...</p>

<pre><code>np.savetxt('array.txt',data,fmt="%i %1.4f")
</code></pre>

<p>I get the following error: "ValueError: fmt has wrong number of % formats:  %i %1.4f"</p>

<p><strong>The Question</strong></p>

<p>Is there a way I say save the first column as integers and the rest of the columns as floating point numbers?</p>

<p>your <code>fmt</code> parameter needs to have the the same number of <code>%</code> as the columns you are trying ot format.  You are trying to format 3 columns but only giving it 2 formats. </p>

<p>Try changing your <code>np.savetxt(...)</code> to</p>

<pre><code>np.savetxt('array.txt',data,fmt="%i %1.4f %1.4f")
</code></pre>

python arrays numpy text-files number-formatting