How do you store an entire array into another array
<p>How do you store an entire array into another array</p>

<p>suppose I have an array </p>

<pre><code>data = np.array([], dtype=float, ndmin=2)
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
</code></pre>

<p>How do you store the values such that</p>

<pre><code>data = [ [1,2,3],
         [4,5,6] ]
</code></pre>

<p>My current method is </p>

<pre><code>data= np.append(data, a)
data= np.append(data, b)
</code></pre>

<p>However this results in [ 1,2,3,4,6]</p>

<p>You mean like:</p>

<pre><code>&gt;&gt;&gt; data = np.array([a,b])
&gt;&gt;&gt; data
array([[1, 2, 3],
       [4, 5, 6]])
</code></pre>

<p>If you want to do it stepwise, you <em>can</em> use append, but you need to make sure all your arguments are rank 2 (or wrapped in a list). Right now, <code>a</code> and <code>b</code> are both rank 1 so if you try to append along a particular axis, you'll get an error. I.e. what you need to do is:</p>

<pre><code>&gt;&gt;&gt; data = np.empty([0,3]); data
array([], shape=(0, 3), dtype=float64)

&gt;&gt;&gt; data = np.append(data, np.array([a]), axis=0); data
array([[ 1.,  2.,  3.]])

&gt;&gt;&gt; data = np.append(data, np.array([b]), axis=0); data
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])
</code></pre>

<p><hr>
PS. However, if the number of rows in <code>data</code> are known (say, 100), you're probably better off preallocating, i.e. initializing it as <code>np.empty([100,3])</code> and filling by index, (e.g. <code>data[0,:] = a</code>)</p>

python arrays numpy