How to add row in NumPy array

In this post, we are going to understand how to add rows in the NumPy array.NumPy array. The numpy library insert(), append(), concatenate() and hstack() is used to add single or multiple elements at once to the end of a numpy array. To run all programs make sure numpy is installed on the local machine.

1. np.insert() to add row in NumPy array

The Numpy library insert() function adds values in the numpy array before the given indices along with the axis. So it takes an index and value that is inserted into the given index. We will add the row at end of the existing array using np.insert() function. We will pass the length of the array to add a row at end of an existing array or can pass index 0 to add a row at the begging of the NumPy array.

import numpy as np 

arr2= np.array([[10,12,23],[24,15,16]]) 

print('original 2D array:\n',arr2)

row = [4,8,12]

#add row at end of existing array
res_Array= np.insert(arr2,len(arr2),row,axis=0)
print('updated  2D array:\n',res_Array)


original array:
 [10 12 23 24 15 16]
Updated array:
 [10 12 23 24 15 16 30 33]

original 2D array:
 [[10 12 23]
 [24 15 16]]
Updated 2D array:
 [[10 12 23]
 [24 15 16]
 [ 4  8 12]]

2. np.append() to add row in NumPy array

The Numpy appends() function adds a row at of NumPy array. This method does not modify the original array else returns a copy of the array after adding the row. In this program, We have appended a row at end of an existing array.

import numpy as np

myarr = np.array([[12,14,700],[60,50,67]])
row= np.array([3,6,9])

#append row at end of array

resarr = np.append(myarr,[row],axis=0)
print('\nNumpy array after append a row:\n ', resarr)


Numpy array after append a row:
  [[ 12  14 700]
 [ 60  50  67]
 [  3   6   9]] to add row in Numpy array

The NumPy array concatenate() function concate two numpy array of same shape either row-wise or column-wise.This function by default concatenate arrays row-wise (axis=0).


numpy.concatenate((a1, a2,, axis=0, out=None, dtype=None)


  • The sequence of array_like objects. The sequence must be of the same shape.
  • axis: The axis along which the array is joined. It is an optional parameter.
    • axis=none: The input array is flattened before use.
    • axis =1: The array is joined column-wise
    • axis = 0: The array is joined row-wise.It is the default value of the axis.
  • Out: optional The output ndarray if the provided output is placed in this array. The shape must be correct.
  • dtype : optional if provided the output array of this type or str.

3.1 Python program to add row in Numpy array

In this python program example, we have to add or concatenate an array at end of the existing 1D numpy array. This is how we use numpy concatenate function to add an array at end of an existing array.

import numpy as np

nparr1 = np.array([[13, 14, 15],
                  [26, 12, 23],
                  [24, 25, 30]])

rows = np.array([ [11, 54, 9],
                    [3, 6, 7],
                    [4, 8, 62]])
# adding rows to numpy array
joined_nparr = np.concatenate( (nparr1, rows))

print('Numpy array after adding rows:\n',joined_nparr)


Numpy array after adding rows:
 [[13 14 15]
 [26 12 23]
 [24 25 30]
 [11 54  9]
 [ 3  6  7]
 [ 4  8 62]]

4. np.vstack() to Add row in NumPy array

The numpy library vstack() function is used to stack arrays vertically row-wise.In this python program example, we have added a row to end the existing array by simply passing a numpy array. We can add single or multiple rows at once at end of a numpy array by passing a numpy array of single or multiple rows using np. vstack() function

import numpy as np

nparr = np.array([[13, 14, 15], [18, 20,21]])

print('original array:\n',nparr)
row = np.array([3,6,9])
resultArr = np.vstack((nparr,row ))

print('Array after adding row:\n',resultArr)


original array:
 [[13 14 15]
 [18 20 21]]
Array after adding row:
 [[13 14 15]
 [18 20 21]
 [ 3  6  9]]