Concatenate a NumPy array to another NumPy array Concatenate a NumPy array to another NumPy array numpy numpy

Concatenate a NumPy array to another NumPy array


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

or this:

In [1]: a = np.array([1, 2, 3])In [2]: b = np.array([4, 5, 6])In [3]: np.vstack((a, b))Out[3]: array([[1, 2, 3],       [4, 5, 6]])


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.


You may use numpy.append()...

import numpyB = numpy.array([3])A = numpy.array([1, 2, 2])B = numpy.append( B , A )print B> [3 1 2 2]

This will not create two separate arrays but will append two arrays into a single dimensional array.