Append/ Add an element to Numpy Array in Python (3 Ways)

Adding an element to Numpy Array in Python

In this article we are going to discuss 3 different ways to add element to the numpy array.

Let’s see one by one method.

Method-1 : By using append() method :

In numpy module of python there is a function numpy.append() which can be used to add an element. We need to pass the element as the argument of the function.

Let’s take a  example where an array is declared first and then we used the append() method to add more elements  to the array.

import numpy as np
arr = np.array([12, 24, 36])
new_Arr = np.append (arr, [48,50,64])
print(‘ array : ’,arr)
print(‘result = : ’ ,new_Arr)
Output :
array : [ 12  24  36 ]
result = : [ 12   24   36  48   50   64 ]

We can also insert a column by the use of  append() method .

Let’s take an example below where we created a 2-Darray and we have to  insert two columns at a specific place.

import numpy as np
arr1= np.array([[12, 24, 36], [48, 50, 64]])
arr2 = np.array([[7], [8]])
new_Array = np.append(arr1,arr2, axis = 1)
print(' array : ',arr1)
print(' result = : ', new_Array)
Output :
array : [ [ 12  24  36  ]  
[ 48  50  64  ] ]
result  : [  [ 12  24  36  7  ] 
[48  50  64   8  ]  ]

We can also insert a row  by the use of  append() method.

Let’s take the  example below where we created a 2-D array and we have to  inserte  a row to it.

import numpy as np 
arr= np.array([[12, 24, 36], [48, 50, 62]])
new_Array = np.append(arr, [[70 ,80 ,90 ]], axis = 0)
print('array = ', arr )
print('result = ' ,new_Array)
Output :
array = [ [ 12  24   36   ]  
[ 48  50    64   ] ]
result = [  [ 12   24   36   ] 
[ 48   50  64   ]
[70   80   90 ] ]

Method-2 : By using concatenate() method :

In numpy module of python there is a function numpy.concatenate() to join two or more arrays. To add a single element we need to encapsulate the single value in a sequence data structure like list pass it to the function.

import numpy as np
# Numpy Array of integers created
arr = np.array([1, 2, 6, 8, 7])
# Adding an element at the end of a numpy array
new_arr = np.concatenate( (arr, [20] ) )
print('array: ', new_arr)
print('result: ', arr)
Output :
array:[1, 2, 6, 8, 7]
result: [1, 2, 6, 8, 7,20]

Adding another array, see the below program

import numpy as np 
arr1 = np.array([[10, 20], [30, 40]])
arr2 = np.array([[50, 60]])
new_Array= np.concatenate((arr1,arr2 ), axis=0)
print( 'First Array : ' ,arr1 )
print( 'Second Array : ' , arr2  )
print( 'concatenated Array : ' , new_Array )
Output :
First Array :   [[ 10  20 ]
                     [30  40 ]]
Second Array :  [[ 50  60 ] ]
 Concatenated Array :     [[ 10   20 ]
                                        [ 30   40 ]
                                        [ 50    60 ]]

Method-3 : By using insert() method :

In numpy module of python there is a function numpy.insert() to add an element at the end of the numpy array.

Below program is to add an elements to the array.

import numpy as np 
arr= np.array (  [ 16, 33, 47, 59, 63 ,79 ])
# Here specified at index 1, so elemnt will eb replaced with new element
new_Array = np.insert(arr, 1, 20 )
print('The array : ', arr)
print ('result :',  new_Array)
Output :
array : [ 16  33  47  59  63  79 ]
result : [ 16  20  33  47  59  66  79 ]