NumPy Append Array in Python

In this Python Numpy Tutorial, I will explain what the NumPy append array in Python is. What is the syntax for the numpy append() function, and how does it work with arrays of different dimensions?

NumPy is a powerful library in Python that provides support for large, multi-dimensional arrays and matrices, along with a collection of high-level mathematical functions to operate on these arrays.

One of the useful functions in NumPy is numpy.append(), which serves to append values to the end of an array. In this article, we will explore the numpy.append() function, its syntax, parameters, and various examples to understand its usage.

Numpy append array in Python

The append function in NumPy adds values to the end of an array in Python. Unlike Python lists, NumPy arrays are fixed in size, meaning that appending elements to an array in Python actually involves creating a new Python numpy array with the additional elements. The numpy.append function returns this new Python array, leaving the original array unchanged.

We will start by creating a new script with the NumPy library as import numpy as np. Next, we will import a new numpy array in Python called data or numbers by calling the np.arange() function and passing the value in the integer data type in Python.

This method will create a NumPy array with integers in Python. Now, we will easily use the append function by using np. append().

Syntax of Numpy append function

The Numpy append() function in Python can be used in the following way:

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

Parameters:

NameDescription
arrIt is the Python numpy array to which values are appended. This could be any array-like structure, including lists in Python.
valuesThese are the values that we want to append to the numpy array in Python. It could also be an array-like structure.
axisThis is an optional parameter. If it is not provided, both the arr and values are flattened before use in Python. Otherwise, values are appended along the specified axis.
Parameters for the np.append() function in Python.

The return value of the numpy array append function

The numpy.append() function returns a new array in Python that includes the original array along with the appended values. It is important to note that this function does not modify the original Python numpy array; instead, it returns a new array with the appended values in Python.

Example:

Let’s take an example to check how the numpy append function is used in Python.

import numpy as np

a = np.array([[1,2,3],[2,6,7]])
b = np.append(a,[6,7,8])
print(b)

Here is the Screenshot of the following given code.

numpy append Python

This is a simple example of the numpy append array in Python.

Append NumPy Array in Python examples

Let’s take some different cases and append values to an array in Python using the append() function.

Case 1: Python numpy append to empty array

  • In this section, we will learn about Python NumPy append to an empty array.
  • The Numpy append method allows us to insert new values to the end of a numpy array.
  • To create an empty array in Python, we use the empty function. it returns an array of given shapes and types without initializing the entries of an array.
  • It is very important to understand unlike zeros empty doesn’t set the array value to zero therefore the performance of the array will be faster than the zero function.
  • It requires a user to manually set all the values of the array.
  • Now we will use the append function by calling np.append(). It is used to append values to the end of a given array.

Example:

import numpy as np

emp_array = np.array([])
arr =([2,3,4])
res = np.append(emp_array,arr)
print(res)

Here is the Screenshot of the following given code.

append to numpy array

This way we can use append() for the numpy append array in Python.

Case 2: Python numpy append 2d array

  • In this section, we will learn about Python numpy append 2d array.
  • The Numpy append function provides us to add new elements to the end of an existing numpy array.
  • A two-dimensional Numpy array means the collection of data in lists of a list. It is also known as a matrix. In a 2D array, you have to use two square brackets which is why it said lists of lists.
  • We will use the function np.reshape(). This method will change the shape of the numbers numpy array into the given number of rows and columns.
  • In NumPy append() 2dimension, we can easily use a function that is reshaping. This reshapes method allows a new shape to a numpy array without changing its data.

Example:

import numpy as np
num = np.arange(4)

b = num.reshape(2,2)
num_2d = (np.append(b,[[4,5]],axis=0))
print(num_2d)

Here is the Screenshot of the following given code

numpy append 2d array Python

This way we can do the numpy append array in Python with the arrange() and reshape() function.

Case 3: Append to numpy array row in Python

  • In this section, we will learn about Python numpy append row.
  • We will use the function numpy.reshape(). This method will change the shape of the numbers numpy array into the given values of rows and columns.
  • In numpy append 2d, we can easily use the function that is np. reshaping. This reshapes function gives a new shape to a numpy array without changing its data.

Example:

import numpy as np
num = np.arange(4)

c = num.reshape(2,2)
add_row = (np.append(c,[[9,8]],axis=0))
print(add_row)

Here is the Screenshot of the following given code.

numpy.append in Python

This is an example of a numpy append array in Python in the row.

Case 4: Append 2d array Python column

  • In this section, we will learn about the Python numpy append column.
  • We will use the method np.reshape(). This function will change the shape of the numpy array into the specified given number of rows and columns.
  • In NumPy append 2d array in Python, we can easily use a function that is np.reshape().
  • This function gives a new shape to a numpy array without changing its data in Python.
  • To append a column in a Python numpy array we use the method np.append().

Example:

import numpy as np
num = np.arange(4)

c = num.reshape(2,2)
new_column = (np.append(c,[[9],[8]],axis=1))
print(new_column)

Here is the Screenshot of the following given code.

Python np.append 2d array

This is an example of a numpy append array in Python in the column.

Case 5: np array append Python

  • In this section, we will learn about the Python NumPy append two arrays.
  • To use this function in Python, you have to make sure that the two numpy arrays have the same length and size.
  • The axis argument specifies the index of the new axis.
  • We will start by declaring a new script with the NumPy library.
  • Next, we will create a new numpy array in Python called numbers or data by calling np. arange() and passing in the integer.
  • This function will declare an array with integers. Now we will use the append function by using np. append().

Example:

import numpy as np

a = np.array([2,3])
b = np.array([4,5])
new_arr = np.append(a,b)
print(new_arr)

Here is the Screenshot of the following given code.

np.append python

This is how to numpy append array in Python using another array.

Case 6: Python NumPy append axis

  • In this section, we will learn about the Python NumPy append axis.
  • We will use the function np.reshape(). This method will change the shape of the numbers numpy array into the specified given number of rows and columns in Python.
  • In numpy append 2d array in Python, we can easily use a function that is the np.reshape().
  • This np.reshape() function gives a new shape to a numpy array without changing its data.

Example:

import numpy as np
num = np.arange(4)

b = num.reshape(2,2)
num_2d = (np.append(b,[[4,5]],axis=0))
print(num_2d)

Here is the Screenshot of the following given code.

np.array append in Python

Case 7: Python NumPy append an element to Array

  • In this section, we will learn about the Python numpy to append an element to the array.
  • The values will be concatenated at the end of the numpy array in Python and a new numpy dimension array will be returned with new and old values.
  • We will start by creating a new script with the NumPy library imported as np.
  • Next, we will create a numpy array called numbers or data by calling np.arange() and passing the integer value in Python.
  • This function will declare a NumPy array in Python with integers.

Example:

import numpy as np

a = np.array([2,3])
b = np.array([4,5])
new_arr = np.append(a,b)
print(new_arr)

Here is the Screenshot of the following given code.

append in python an element to the array

This is how to append an element to an array in Python NumPy.

Case 8: Python NumPy append not working

  • In this section, we will learn about the Python numpy append not working.
  • To use this method, you have to make sure that the two numpy arrays have the same length and size.

Example:

import numpy as np

a = np.array([1])
print(a)
np.append(a, [2])
print(a)

Here is the Screenshot of the following given code.

np.append not working

This is how the numpy append array in Python not working.

Case 9: Python NumPy append array to array

  • In this section, we will learn about the Python numpy append array to array.
  • The axis parameter specifies the index of the given axis.
  • We will start by declaring a new script with the NumPy library.
  • Next, we will create a new numpy array called numbers by using numpy.arange() and passing in the integer value which will declare a NumPy array with integers.
  • Now we will use the append function by using numpy.concatenate().

Example:

import numpy as np

a = np.array([2,3])
b = np.array([4,5])
new_arr = np.concatenate((a,b),axis=0)
print(new_arr)

Here is the Screenshot of the following given code.

python append array

This is an example of a numpy append array in Python with array.

Conclusion

This Python tutorial explains what the NumPy append array in Python is in detail with its syntax, parameters required, and what it returns. I have explained the use of the append() function with different user cases.

Understanding each and every case in detail can help one in solving their problems with ease.

You may also like to read some of our NumPy articles: