I just started programming in python and am very new to numpy packages.. so still trying to get a hang of it. I have a an numpy_array so something like [ a b c]

And then I want to append it into anotehr numpyarray (Just like we create a list of lists) How do we create an array of numpy arrays containing numpy arrays I tried to do the following without any luck

>>> M = np.array([])
>>> M
array([], dtype=float64)
>>> M.append(a,axis=0)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AttributeError: 'numpy.ndarray' object has no attribute 'append'
>>> a
array([1, 2, 3])
link|improve this question

1  
You can create an "array of arrays" (you use an object array), but you almost definitely don't want to. What are you trying to do? Do you just want a 2d array? – Joe Kington Mar 19 at 18:00
yeah.. I am trying to get a 2D array – Fraz Mar 19 at 18:04
@Fraz: Why do you want a 2D array? What are you trying to do? – endolith Mar 19 at 18:15
feedback

3 Answers

up vote 2 down vote accepted
In [1]: import numpy

In [2]: a = array([[1,2,3],[4,5,6]])

In [3]: b = array([[9,8,7],[6,5,4]])

In [4]: numpy.concatenate((a,b))
Out[4]: 
array([[1, 2, 3],
       [4, 5, 6],
       [9, 8, 7],
       [6, 5, 4]])

or this:

In [1]: a = array([1,2,3])

In [2]: b = array([4,5,6])

In [3]: numpy.vstack((a,b))
Out[3]: 
array([[1, 2, 3],
       [4, 5, 6]])
link|improve this answer
Hi when i run this i get this np.concatenate((a,b),axis=1) Output: array([1, 2, 3, 2, 3, 4]) But what I looking for is numpy 2d array?? – Fraz Mar 19 at 18:05
@Fraz: I've added Sven's vstack() idea. You know you can create the array with array([[1,2,3],[2,3,4]]), right? – endolith Mar 19 at 18:14
feedback

Sven said it all, just be very cautious because of automatic type adjustments when append is called.

In [2]: import numpy as np

In [3]: a = np.array([1,2,3])

In [4]: b = np.array([1.,2.,3.])

In [5]: c = np.array(['a','b','c'])

In [6]: np.append(a,b)
Out[6]: array([ 1.,  2.,  3.,  1.,  2.,  3.])

In [7]: a.dtype
Out[7]: dtype('int64')

In [8]: np.append(a,c)
Out[8]: 
array(['1', '2', '3', 'a', 'b', 'c'], 
      dtype='|S1')

As you see based on the contents the dtype went from int64 to float32, and then to S1

link|improve this answer
feedback

Well, the error message says it all: NumPy arrays do not have an append() method. There's a free function numpy.append() however:

numpy.append(M, a)

This will create a new array instead of mutating M in place. Note that using numpy.append() involves copying both arrays. You will get better performing code if you use fixed-sized NumPy arrays.

link|improve this answer
Hi.. when i try this.. I get this >>> np.append(M,a) array([ 1., 2., 3.]) >>> np.append(M,b) array([ 2., 3., 4.]) >>> M array([], dtype=float64) I was hoping M to be a 2D array?? – Fraz Mar 19 at 18:06
4  
@Fraz: Have a look at numpy.vstack(). – Sven Marnach Mar 19 at 18:08
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.