Numpy - add row to array

Darren J. Fitzpatrick picture Darren J. Fitzpatrick · Oct 7, 2010 · Viewed 467.6k times · Source

How does one add rows to a numpy array?

I have an array A:

A = array([[0, 1, 2], [0, 2, 0]])

I wish to add rows to this array from another array X if the first element of each row in X meets a specific condition.

Numpy arrays do not have a method 'append' like that of lists, or so it seems.

If A and X were lists I would merely do:

for i in X:
    if i[0] < 3:
        A.append(i)

Is there a numpythonic way to do the equivalent?

Thanks, S ;-)

Answer

jknair picture jknair · Oct 7, 2010

well u can do this :

  newrow = [1,2,3]
  A = numpy.vstack([A, newrow])