Append/ Add an element to Numpy Array in Python (3 Ways)

In this article, we will discuss different ways to add / append single element in a numpy array by using append() or concatenate() or insert() function.

Table of Contents

Add element to Numpy Array using append()

Numpy module in python, provides a function to numpy.append() to add an element in a numpy array. We can pass the numpy array and a single value as arguments to the append() function. It doesn’t modifies the existing array, but returns a copy of the passed array with given value added to it. For example,

import numpy as np

# Create a Numpy Array of integers
arr = np.array([11, 2, 6, 7, 2])

# Add / Append an element at the end of a numpy array
new_arr = np.append(arr, 10)

print('New Array: ', new_arr)
print('Original Array: ', arr)

Output:

New Array:  [11  2  6  7  2 10]
Original Array:  [11  2  6  7  2]

The append() function created a copy of the array, then added the value 10 at the end of it and final returned it.

Advertisements

Add element to Numpy Array using concatenate()

Numpy module in python, provides a function numpy.concatenate() to join two or more arrays. We can use that to add single element in numpy array. But for that we need to encapsulate the single value in a sequence data structure like list and pass a tuple of array & list to the concatenate() function. For example,

import numpy as np

# Create a Numpy Array of integers
arr = np.array([11, 2, 6, 7, 2])

# Add / Append an element at the end of a numpy array
new_arr = np.concatenate( (arr, [10] ) )

print('New Array: ', new_arr)
print('Original Array: ', arr)

Output:

New Array:  [11  2  6  7  2 10]
Original Array:  [11  2  6  7  2]

It returned a new array containing values from both sequences i.e. array and list. It didn’t modified the original array, but returned a new array containing all values from original numpy array and a single value added along with them in the end.

Add element to Numpy Array using insert()

Using numpy.insert() function in the NumPy module, we can also insert an element at the end of a numpy array. For example,
C
Output:
O

We passed three arguments to the insert() function i.e. a numpy array, index position and value to be added. It returned a copy of array arr with value added at the given index position. As in this case we wanted to add the element at the end of array, so as the index position, we passed the size of array. Therefore it added the value at the end of array.

Important point is that it did not modifies the original array, it returned a copy of the original array arr with given value added at the specified index i.e. as the end of array.

Summary:

We learned about three different ways to append single element at the end of a numpy array in python.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

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

Scroll to Top