Add Tuple To NumPy Array in Python

In this tutorial, you’ll learn several ways to add tuples to NumPy arrays.

We’ll cover appending tuples to 1D arrays, inserting them into specific positions, or integrating them into 2D arrays as rows or columns.

 

 

Using numpy.append()

You can use numpy.append() function to add a tuple to the array:

import numpy as np
array = np.array([1, 2, 3])
tuple_to_add = (4, 5)
updated_array = np.append(array, tuple_to_add)
print(updated_array)

Output:

[1 2 3 4 5]

numpy.append() adds the elements of the tuple to the end of the array.

 

Using numpy.concatenate()

You can use numpy.concatenate() to add multiple tuples at once to a NumPy array.

import numpy as np
array = np.array([6, 7, 8])
tuple1 = (9, 10)
tuple2 = (11, 12)
updated_array = np.concatenate((array, np.array(tuple1), np.array(tuple2)))
print(updated_array)

Output:

[ 6  7  8  9 10 11 12]

 

Using numpy.insert()

numpy.insert() allows you to insert elements into an array at a specific position or along a given axis while maintaining the array’s shape.

Let’s say you want to insert at position 1 (indexing starts at 0):

import numpy as np
array = np.array([21, 22, 23])
tuple_to_insert = (24, 25)
updated_array = np.insert(array, 1, tuple_to_insert)
print("Updated Array:", updated_array)

Output:

Updated Array: [21 24 25 22 23]

In this example, numpy.insert() has inserted the elements of the tuple at the specified index and shifted the other elements to the right.

 

Add a Tuple at the Beginning or End of NumPy Array

There are scenarios where you might want to add a tuple either at the beginning or at the end of a NumPy array.

Add a Tuple at the Beginning

First, let’s import NumPy and create an initial array:

import numpy as np
array = np.array([26, 27, 28])
tuple_at_start = (29, 30)
array_with_tuple_at_start = np.insert(array, 0, tuple_at_start)
print("Array with Tuple at Start:", array_with_tuple_at_start)

Output:

Array with Tuple at Start: [29 30 26 27 28]

Add a Tuple at the End

To append a tuple at the end of the array, numpy.append() is the most straightforward method:

tuple_at_end = (31, 32)
array_with_tuple_at_end = np.append(array, tuple_at_end)
print("Array with Tuple at End:", array_with_tuple_at_end)

Output:

Array with Tuple at End: [26 27 28 31 32]

 

Add a Tuple to 2D NumPy Array as a Row or Column

Adding a tuple as either a new row or a new column to a 2D NumPy array is a common operation in data manipulation.

This is relevant in cases where you’re working with matrix-like data structures.

Add a Tuple as a New Row

To add a tuple as a new row, you can use numpy.vstack():

import numpy as np
array_2d = np.array([[33, 34], [35, 36]])
tuple_as_row = (37, 38)
updated_array_row = np.vstack((array_2d, tuple_as_row))
print("Updated Array with New Row:\n", updated_array_row)

Output:

Updated Array with New Row:
 [[33 34]
 [35 36]
 [37 38]]

Add a Tuple as a New Column

You can use numpy.hstack() to add a tuple as a new column:

tuple_as_column = ([39], [40])
updated_array_column = np.hstack((array_2d, tuple_as_column))
print("Updated Array with New Column:\n", updated_array_column)

Output:

Updated Array with New Column:
 [[33 34 39]
 [35 36 40]]

Here, numpy.hstack() is used to add the tuple as a new column to the right side of the 2D array.

Leave a Reply

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