How to extend NumPy array in Python

In this post, we are going to learn how to extend the NumPy array in Python. To extend elements to the NumPy array we pass the element or array that we want to extend to the NumPy array.

Ways to extend NumPy array in Python


  • Extend element to Numpy Array append().
  • Extend NumPy array row-wise in Python.
  • Extend NumPy array column-wise.
  • Extend NumPy array with zeros.
  • Extend NumPy array with same value.


Numpy Array append()


The Numpy appends() function adds an element in a NumPy array at the end of the array. This method does not modify the original array else returns a copy of the array after adding the passed element or array.

Syntax

numpy.append(arr,values,axis=none)

Parameters

  • arr: The copy of the array in which values are to be appended.
  • values: The values adding or append to array.The shape(size) must be same as arr exclude axis.If axis is not passed the value can be any shape and flatten before use.
  • axis: The axis represent adding values row-wise or columns-wise.if none both array or values are flatten before use.

1. Extend an element to NumPy array using append()


In this Python program, we have extended an element to the NumPy array by using the append() function. This is how we extend an element in the NumPy array.

Python Program to extend an element to NumPy array

import numpy as np
 
 
myarr = np.array([12,14,700,60,50])
]
 
#extend an element at end of array
 
 
resarr = np.append(myarr, 90)
print('extend an element to Numpy array: ', resarr)
 


Output

append an element to Numpy  array:  [ 12  14 700  60  50  90]

2. How to Extend NumPy array row-wise in Python


We have a two-dimensional numpy array and extend an array using numpy append() function row-wise bypassing axis =0.

  • To use numpy function first need to import NumPy libraray “import numpy as np”
  • The resarr is array after extended
  • Printing the result array using print() method

Python Program to extend 2D NumP array row-wise

import numpy as np
 
 
myarr = np.array([[12,14,700],[20,75,60]])


appendarr = [[15,16,17]]


#extend array to Numpy array
 
resarr = np.append(myarr, appendarr,axis=0)
print('extend element row-wise Numpy array: ', resarr)
 

Output

extend element row-wise Numpy array:  [[ 12  14 700]
 [ 20  75  60]
 [ 15  16  17]]

3. Extend NumPy array column-wise in Python


In this Python program, We are extending a numpy array using append() function column-wise bypassing axis =1.

Python Program to extend 2D NumP array column-wise

import numpy as np
 
 
myarr = [[15,16,17],[23,24,26]]

appendarr = [[12,14,700],[20,75,60]]
 
 
resarr = np.append(myarr, appendarr,axis=1)
print('extend element column-wise Numpy array: ', resarr)
 

Output

extend element column-wise Numpy array:  [[ 15  16  17  12  14 700]
 [ 23  24  26  20  75  60]]

4. Extend Numpy array with zeros


In this Python program, we are extending a 1D and 2D Numpy array with zeros by using the np.pad() function. This is how we extend the NumPy array with zeros.

Python Program to extend NumPy array with zeros

import numpy as np
 
myarr = np.array([3,6,9])

#extend 1D NumPy  array
pad_1Darr = np.pad(myarr, (2, 3), 'constant')

print('1D zero extend array:\n',pad_1Darr)


#extend 2D NumPy  array
myarr2D = np.array([[4,8],[12,16]]) # 2D array
pad_2Darr =np.pad(myarr2D, (2, 2), 'constant') # Pad 2 left, 2 right
print('\n 2D zero extend array:\n',pad_2Darr)


Output

 2D zero extend array:
 [[ 0  0  0  0  0  0]
 [ 0  0  0  0  0  0]
 [ 0  0  4  8  0  0]
 [ 0  0 12 16  0  0]
 [ 0  0  0  0  0  0]
 [ 0  0  0  0  0  0]]

5. Extend NumPy array with same value


To extend the NumPy array with the same value. The numpy.full() function is used to create an array of a given shape that contains the same values. Let us understand with examples how to extend the NumPy array with the same values.

Python Program to extend NumPy array with same values

import numpy as np
nparr = np.full((2,3), 6)
print(nparr)

Output

[[6 6 6]
 [6 6 6]]

Summary

In this post we have learnt how to extend NumPy array in Python by usinh numpy.append(),numpy.pad() to extend with zero and nump.full() to extend with same values