numpy.append() – Python

In this article, we will discuss how to append elements at the end on a Numpy Array in python using numpy.append()

Overview of numpy.append()

Python’s Numpy module provides a function to append elements to the end of a Numpy Array.

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

Arguments:

  • arr: array_like
    • Given values will be added in copy of this array.
  • values: array_like
    • Values that needs to be added in the array.
    • If axis is provided, then values to be added must be of similar shape as array arr along the axis where we want to add.
  • axis: int, optional
    • The axis along which values will be added to array. Default value is None.
      • If axis is None: Then values array will be flattened and added to the array arr.
      • If axis is 0, then values will be appended row wise.
      • If axis is 1, then values will be appended column wise.

Returns:

  • A copy of the given array arr, with values appended to the array.

It doesn’t modify the original array in parameter arr. It creates a copy of this array and appends the elements from values parameter, to the end of this new copied array. So, basically it returns a copy of numpy array provided with values appended to it.

Let’s understand by examples :

Append elements at the end of 1D numpy array

Let’s create a Numpy array i.e.

import numpy as np

# create a Numpy array from a list
arr = np.array([1, 2, 3, 4, 5, 6, 7])

Append a single element to the Numpy array

# Append a single element at the end of Numpy Array
newArr = np.append(arr, 88)

Contents of the new Numpy Array returned :

[ 1  2  3  4  5  6  7 88]

Now let’s see how append multiple elements to a Numpy array.

Append elements from a list to the Numpy array

import numpy as np

# create a Numpy array from a list
arr = np.array([1, 2, 3, 4, 5, 6, 7])

# Append multiple elements from a list to the end of a Numpy Array
newArr = np.append(arr, [88,99,100])

Contents of the new Numpy Array returned :

[  1   2   3   4   5   6   7  88  99 100]

Flatten 2D Numpy Array and add items to it

Let’s create a 2D numpy array i.e.

import numpy as np

# Create a 2D Numpy Array like Matrix
matrixArr = np.array( [ [1, 2, 3],
                        [ 4, 5, 6] ])

Now append 1D list to this 2D Numpy array.

# Add elements in List to 2D Numpy array by flattening
newArr = np.append(matrixArr, [22, 23, 24])

As axis parameter is not provided in call to append(), so both the arrays will be flattened first and then values will appended. Therefore, contents of the new flattened Numpy Array returned are,

[ 1  2  3  4  5  6 22 23 24]

Add a Numpy Array to another array row wise

If we provide axis parameter in append() call then both the arrays should be of same shape. Let’s create two 2D numpy arrays,

import numpy as np

# Create two 2D Numpy Array like Matrix
matrixArr1 = np.array([[1, 2, 3],
                        [4, 5, 6]])

matrixArr2 = np.array([[70, 80, 90],
                         [61, 62, 63]])

Now let’s append the rows from a numpy array to the end of another numpy array by passing axis as 0 i.e.

newArr = np.append(matrixArr1, matrixArr2 , axis=1)

Contents of 2D array matrixArr2 will be appended to the contents of matrixArr1  as rows in new array. Contents of the returned array are,

[[1 2 3]
 [4 5 6]
 [1 2 3]
 [4 5 6]]

Add a NumPy Array to another array – Column Wise

In the above example if instead of passing axis as 0 we pass axis=1 then contents of 2D array matrixArr2 will be appended to the contents of matrixArr1  as columns in new array i.e.

import numpy as np

# Create two 2D Numpy Array like Matrix
matrixArr1 = np.array([[1, 2, 3],
                        [4, 5, 6]])

matrixArr2 = np.array([[70, 80, 90],
                         [61, 62, 63]])

newArr = np.append(matrixArr1, matrixArr2 , axis=1)

Contents of the new Numpy Array returned are,

[[ 1  2  3 70 80 90]
 [ 4  5  6 61 62 63]]

Error while appending elements in Numpy array of different shapes

If you are providing axis parameter in numpy.append() then both the arrays should be of same shape along the given axis, otherwise it will raise Error. For example,

Let’s try to append a 1D array to 2D array with axis = 1 i.e.

import numpy as np

# Create a 2D Numpy Array like Matrix
matrixArr = np.array( [ [1, 2, 3],
                        [ 4, 5, 6] ])

arr5 = np.append(matrixArr, [22, 23, 24], axis=1 )

It will give following error,

ValueError: all the input arrays must have same number of dimensions

We are trying to row wise append 1D array to a 2D array of shape 2X3, shapes are not compatible therefore, it gave the error. We should make the shape NX3 where N can be anything greater than 1.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top