NumPy: How to insert a row/column into a 2D array

Updated: January 23, 2024 By: Guest Contributor Post a comment

Introduction

NumPy is an essential library for scientific computing in Python. It provides a high-performance multidimensional array object and tools for working with these arrays. A common operation when dealing with matrices and linear algebra is inserting rows or columns into an existing array. In this tutorial, we explore various techniques to do this effectively using NumPy.

Understanding NumPy Arrays

Before we delve into inserting elements, it’s important to understand that a NumPy array, also known as ndarray, is a grid of values, all of the same type, that is indexed by a tuple of nonnegative integers. Modifying the shape of an existing array will return a new array without affecting the original one.

Basic Row and Column Insertion

Let’s begin with the basic operations of inserting a row or column.

import numpy as np

# Creating a 2D array
my_array = np.array([[1, 2, 3], [4, 5, 6]])

# Row to insert
new_row = np.array([7, 8, 9])

# Inserting the new row at index 1
my_array_updated = np.insert(my_array, 1, new_row, axis=0)

print(my_array_updated)

The output will be:

[[1 2 3]
 [7 8 9]
 [4 5 6]]

This piece of code demonstrates insertion of a new row into a 2D array using the np.insert() function. By setting axis=0, we indicate that the insertion is along the row.

Now, let’s insert a column:

# Column to insert
new_column = np.array([7, 8])

# Inserting the new column at index 2
my_array_updated = np.insert(my_array, 2, new_column, axis=1)

print(my_array_updated)

The output:

[[1 2 7]
 [4 5 8]]

We add a new column by setting axis=1. The new column is inserted before the column at index 2.

Working with Slicing and Broadcasting

In some situations, we might want to insert a row or column containing repeated elements. We can accomplish this using slicing and broadcasting.

# Insert a row of zeros at index 1
rows, cols = my_array.shape
zeros_row = np.zeros((1, cols))

my_array_zerorow = np.insert(my_array, 1, zeros_row, axis=0)

print(my_array_zerorow)

Output:

[[1 2 3]
 [0 0 0]
 [4 5 6]]

You will notice that we generated a row of zeros that has the same number of columns as ‘my_array’ and used the np.insert() function to place it into the original array.

Inserting Multiple Rows or Columns

NumPy also enables us to insert multiple rows or columns at once.

# Insert multiple rows
new_rows = np.array([[7, 8, 9],[10, 11, 12]])
my_array_multirow = np.insert(my_array, [1], new_rows, axis=0)

print(my_array_multirow)

Output:

[[ 1  2  3]
 [ 7  8  9]
 [10 11 12]
 [ 4  5  6]]

By passing a list with a single index to the np.insert() function, all the rows in the new_rows array are added at that position.

Conditions for Insertion

In advanced applications, we may want to integrate conditions for insertion or manipulate multiple arrays simultaneously.

Let’s assume we want to insert values based on a specific condition. One way to do this is by using NumPy’s indexing capabilities alongside np.insert(), like so:

# Inserting an element on condition
mask = (my_array % 2 == 0)
modified_array = np.copy(my_array)
modified_array[mask] = np.insert(modified_array[mask], 2, 999)

print(modified_array)

Conclusion

NumPy’s insertion operations are powerful tools that allow you to manipulate array data in complex ways, making it a staple for any data manipulation, machine learning, or scientific computation task in Python. This tutorial has provided a glimpse into the capabilities of NumPy for inserting rows and columns, by focusing primarily on the np.insert() function, and it has offered insight into how these functions can be applied in different contexts.