Signup/Sign In
LAST UPDATED: DECEMBER 7, 2020

NumPy append() function

In this tutorial, we will cover the numpy.append() function of the NumPy library.

The "append" word simply means to add something to the existing data at the end or at the last.

  • In the NumPy library, the append() function is mainly used to append or add something to an existing array.

  • This function always append the values at the end of the array and that too along the mentioned axis.

  • The append() function is mainly used to merge two arrays and return a new array as a result.

  • During this operation the original array remains unchanged.

Syntax of numpy.append():

The syntax required to use this function is as follows:

numpy.append(a, values, axis=None)

Parameters:

Let us take a look at the parameters of this function:

  • a
    This parameter indicates the ndarray to which the new values will be appended and a new array will be created.

  • values
    This parameter is mainly used to define the values that are needed to append to the copy of a (the existing ndarray). It is important to note here that these values must be of the correct shape just like the original ndarray, only excluding the axis. In the case if the axis is not defined, then the values can be in any shape and will be flattened before the use.

  • axis
    This is an optional parameter and is used to define the axis along which the values are appended. When the axis is not given then in that case both ndarray and values are flattened before use.

Returned Values:

The append() function returns the copy of the ndarray along with the values appended to its axis.

Example 1: Basic usage

Let us have a look at the basic example of this function and the code snippet for the same is as follows:

import numpy as np  

a = np.array([[1, 2, 3], [7, 8, 9]])  
b = np.array([[11, 21, 31], [42, 52, 62]])  

print("The first array:\n",a)
print("The second array:\n",b)
print("The resultant array after appending a & b:")
c = np.append(a,b)  
c  

The output of the above code will be:

numpy append() function example

Example 2: With axis=0

Let us have a look at another case where we will take axis=0 and check the output:

import numpy as np  
a=np.array([[1, 2, 3], [7, 8, 9]])  
b=np.array([[11, 21, 31], [42, 52, 62]])  
print("The first array:\n",a)
print("The second array:\n",b)
print("The resultant array after appending a & b:")
c=np.append(a,b,axis=0)  
c  

The output of the above code will be:

numpy append() function example

Example 3: With axis=1

Let us have a look at another case where we will take axis=1 and check the output:

import numpy as np  
a=np.array([[1, 2, 3], [7, 8, 9]])  
b=np.array([[11, 21, 31], [42, 52, 62]])  
print("The first array:\n",a)
print("The second array:\n",b)
print("The resultant array after appending a & b:")
c=np.append(a,b,axis=1)  
c  

The output of the above code will be:

numpy append() function example

Summary

This tutorial was all about append() function in the Numpy library. If you want to create a new array, using an existing array with some new values added to it, then the numpy.append() function should be used.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.