numpy.append() in Python

The numpy.append() function is available in NumPy package. As the name suggests, append means adding something. The numpy.append() function is used to add or append new values to an existing numpy array. This function adds the new values at the end of the array.

The numpy append() function is used to merge two arrays. It returns a new array, and the original array remains unchanged.

Syntax

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

Parameters

There are the following parameters of the append() function:

1) arr: array_like

This is a ndarray. The new values are appended to a copy of this array. This parameter is required and plays an important role in numpy.append() function.

2) values: array_like

This parameter defines the values which are appended to a copy of a ndarray. One thing is to be noticed here that these values must be of the correct shape as the original ndarray, excluding the axis. If the axis is not defined, then the values can be in any shape and will flatten before use.

3) axis: int(optional)

This parameter defines the axis along which values are appended. When the axis is not given to them, both ndarray and values are flattened before use.

Returns

This function returns a copy of ndarray with values appended to the axis.

Example 1: np.append()

snippet
import numpy as np
a=np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])
b=np.array([[11, 21, 31], [42, 52, 62], [73, 83, 93]])
c=np.append(a,b)
c

Output:

Output
array([ 10, 20, 30, 40, 50, 60, 70, 80, 90, 11, 21, 31, 42, 52, 62, 73, 83, 93])

In the above code

  • We have imported numpy with alias name np.
  • We have created an array 'a' using np.array() function.
  • Then we have created another array 'b' using the same np.array() function.
  • We have declared the variable 'c' and assigned the returned value of np.append() function.
  • We have passed the array 'a' and 'b' in the function.
  • Lastly, we tried to print the value of arr.

In the output, values of both arrays, i.e., 'a' and 'b', have been shown in the flattened form, and the original array remained same.

Example 2: np.append({a1,a2,...}, axis=0)

snippet
import numpy as np
a=np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])
b=np.array([[11, 21, 31], [42, 52, 62], [73, 83, 93]])
c=np.append(a,b,axis=0)
c

In the above code

  • We have imported numpy with alias name np.
  • We have created an array 'a' using np.array() function.
  • Then we have created another array 'b' using the same np.array() function.
  • We have declared the variable 'c' and assigned the returned value of np.append() function.
  • We have passed the array 'a' and 'b' in the function, and we have also passed the axis as 0.
  • Lastly, we tried to print the value of arr.

In the output, values of both arrays, i.e., 'a' and 'b', have been shown vertically in a single array, and the original array remained the same.

Output:

Output
array([[ 10, 20, 30], [ 40, 50, 60], [ 70, 80, 90], [11, 21, 31], [42, 52, 62], [73, 83, 93]])

Example 3: np.append({a1,a2,...}, axis=1)

snippet
import numpy as np
a=np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])
b=np.array([[11, 21, 31], [42, 52, 62], [73, 83, 93]])
c=np.append(a,b,axis=1)
c

Output:

Output
array([[ 10, 20, 30, 11, 21, 31], [ 40, 50, 60, 42, 52, 62], [ 70, 80, 90, 73, 83, 93]])
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +