numpy.append() – The NumPy append Python Function

Introduction to NumPy append

The numpy.append() appends values along the mentioned axis at the end of the array.

Syntax:

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

Parameters:

  • array : [array_like] Input array.
  • values : [array_like] values to be added in the arr. Values should be shaped so that arr[…, obj,…] = values. If the axis is define values can be of any shape as it will be flatten before use.
  • axis: Axis along which we want to insert the values. By default, an array is flatten.

Return: An copy of the array with values being appended at the end as per the mentioned object along a given axis.

Code Examples of NumPy append

Example 01: Appending Arrays

# welcome to softhunt.net
# Python Program illustrating
# numpy.append()

import numpy as np

#Working on 1D
arr1 = np.arange(5)
print("1D arr1 : ", arr1)
print("Shape : ", arr1.shape)


arr2 = np.arange(8, 12)
print("\n1D arr2 : ", arr2)
print("Shape : ", arr2.shape)


# appending the arrays
arr3 = np.append(arr1, arr2)
print("\nAppended arr3 : ", arr3)

Output:

1D arr1 :  [0 1 2 3 4]
Shape :  (5,)

1D arr2 :  [ 8  9 10 11]
Shape :  (4,)

Appended arr3 :  [ 0  1  2  3  4  8  9 10 11]

Example 02: Working with axis

# welcome to softhunt.net
# Python Program illustrating
# numpy.append()

import numpy as np

#Working on 1D
arr1 = np.arange(8).reshape(2, 4)
print("2D arr1 : \n", arr1)
print("Shape : ", arr1.shape)


arr2 = np.arange(8, 16).reshape(2, 4)
print("\n2D arr2 : \n", arr2)
print("Shape : ", arr2.shape)


# appending the arrays
arr3 = np.append(arr1, arr2)
print("\nAppended arr3 by flattened : ", arr3)

# appending the arrays with axis = 0
arr3 = np.append(arr1, arr2, axis = 0)
print("\nAppended arr3 with axis 0 : \n", arr3)

# appending the arrays with axis = 1
arr3 = np.append(arr1, arr2, axis = 1)
print("\nAppended arr3 with axis 1 : \n", arr3)

Output:

2D arr1 : 
 [[0 1 2 3]
 [4 5 6 7]]
Shape :  (2, 4)

2D arr2 : 
 [[ 8  9 10 11]
 [12 13 14 15]]
Shape :  (2, 4)

Appended arr3 by flattened :  [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15]

Appended arr3 with axis 0 : 
 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

Appended arr3 with axis 1 : 
 [[ 0  1  2  3  8  9 10 11]
 [ 4  5  6  7 12 13 14 15]]

FAQs

What is the NumPy library in Python?

NumPy is a Python module that allows you to interact with arrays. It also provides functions for working with matrices, Fourier transforms, and linear algebra. Travis Oliphant invented NumPy in 2005. It is an open-source project that you are free to use. 

Is NumPy an API?

The C-API in NumPy allows users to extend the system and gain access to the array object for usage in other procedures. Reading the source code is the best method to properly understand the C-API. However, if you’re not use to working with (C) source code, this might be intimidating at first. Be sure that with experience, the process will become simpler, and you may be amaze at how easy C-code may be to understand. Even if you don’t think you’ll be able to write C code from scratch, it’s a lot easier to understand and alter existing code than it is to write it from scratch.

Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

  1. numpy.insert() – The NumPy insert Python Function

Leave a Comment