HOWTO · NumPy

How to Add a Row to a Matrix in NumPy

This tutorial demonstrates how to add a row to a matrix in NumPy, covering methods like vstack, append, and concatenate. Learn how to manipulate matrices efficiently and enhance your data analysis skills with practical code examples.

On this page

A NumPy matrix is a two-dimensional array. To add a row, provide values for every column and combine that row along axis 0. Use numpy.vstack() for a direct vertical stack; use numpy.append() or numpy.concatenate() when their array-combining behavior better fits your code. These operations return a new array, so assign the result if you want to keep it.

The examples use a matrix with three columns and add [7, 8, 9]. Each example prints the resulting array so you can see the added row.

Add a row with numpy.vstack()

numpy.vstack() stacks arrays vertically. It accepts the new row as a one-dimensional array and returns a new array with that row at the bottom.

Run this example:

import numpy as np

matrix = np.array([[1, 2, 3],
                   [4, 5, 6]])
new_row = np.array([7, 8, 9])

result = np.vstack((matrix, new_row))
print(result)

The original matrix remains unchanged; result contains the added row. The row must have three values to match the matrix’s column count. For several rows, pass a two-dimensional array of rows to numpy.vstack().

Add a row with numpy.append()

For numpy.append(), set axis=0 to append along the row dimension. In this example the new row is two-dimensional, with shape (1, 3), matching the matrix’s two dimensions and three columns.

import numpy as np

matrix = np.array([[1, 2, 3],
                   [4, 5, 6]])
new_row = np.array([[7, 8, 9]])

result = np.append(matrix, new_row, axis=0)
print(result)

numpy.append() returns a new array rather than changing matrix in place. Assign the return value, as above, to use the appended array. With axis=0, the arrays must have the same number of columns; this operation can also append multiple rows when they are supplied with a compatible two-dimensional shape. For repeated additions to a large array, consider collecting rows and combining them once, since each append constructs a result array.

Combine rows with numpy.concatenate() and numpy.r_

numpy.concatenate() joins arrays along an existing axis. Use axis=0 to add rows, and make sure each input has the same column count. This is a general array-combination operation, so it also works when combining more than two compatible arrays.

import numpy as np

matrix = np.array([[1, 2, 3],
                   [4, 5, 6]])
new_row = np.array([[7, 8, 9]])

result = np.concatenate((matrix, new_row), axis=0)
print(result)

numpy.r_ is a special indexing object, not a function call. It offers concise row-wise combination for this case:

import numpy as np

matrix = np.array([[1, 2, 3],
                   [4, 5, 6]])
new_row = np.array([[7, 8, 9]])

result = np.r_[matrix, new_row]
print(result)

Choose numpy.vstack() when vertical stacking makes the intent clearest, and numpy.concatenate() when you want to name the axis explicitly or combine a sequence of arrays. numpy.r_ is a compact alternative for simple concatenation.

Insert a row at a specific position with numpy.insert()

Use numpy.insert() when the new row belongs between existing rows. Its obj argument identifies the insertion position: index 1 places the row before the current second row. Set axis=0 so NumPy inserts along the row dimension.

import numpy as np

matrix = np.array([[1, 2, 3],
                   [4, 5, 6]])
new_row = [7, 8, 9]

result = np.insert(matrix, 1, new_row, axis=0)
print(result)

To insert after the last row, use the current row count as the position. For a two-dimensional array, .shape is an attribute whose first value is the number of rows:

import numpy as np

matrix = np.array([[1, 2, 3],
                   [4, 5, 6]])
new_row = [7, 8, 9]

position = matrix.shape[0]
result = np.insert(matrix, position, new_row, axis=0)
print(result)

As with the other methods, numpy.insert() returns a new array. Keep the result or assign it back to matrix; the original array is not modified. In all four approaches, the added row needs the same number of columns as the existing rows.