Add Row to NumPy Array in Python

In this article, we will learn how to add a row to a 2D NumPy Array in python.

Given a NumPy array, we need to add a row to the array. For example,

Example:             
	
Given array:

[[1 2 3 4 5 ],
[5 4 3 2 1 ]]

row = [ 6 7 8 9 1 ]

After adding row to the array:

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

There are multiple ways to Add a Row to a NumPy Array. Let’s discuss all the methods one by one with a proper approach and a working code example

1. Using append() method to Add a Row to a NumPy Array

Numpy module in python, provides a function numpy.append() to append objects to the end of an array, The object should be an array like entity. The append() method will take an array, object to be appended as arguments. It returns a copy of the numpy array, with given values appended at the end.

Syntax of append()

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

Parameters:

arr          = The array to be passed to the function.
values       = array_like object to appended to the array.
axis         = int, optional, Axis along which to append values.

Return:

Returns array with values appended at the end.

In this case, to add a row to a 2D NumPy array we need to pass the numpy array and row to the append() method and set the axis = 0. It will return a copy of array with the added row.

Approach

  1. Import numpy library and create a numpy array
  2. Pass the array, row to be added to the append() method and set axis=0.
  3. The append() method will return copy of the array by adding the row.
  4. Print the new array

Source code

import numpy as np

# creating  numpy array
arr = np.array([[1, 2, 3, 4, 5],
                [5, 4, 3, 2, 1]])

row = np.array([6, 7, 8, 9, 1])
        
# Adding row to array using append() method
arr = np.append(arr, [row], axis=0)

# Array after adding the row.
print(arr)

OUTPUT:

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

2. Using concatenate() method to Add a Row to a NumPy Array

Numpy module in python, provides a function numpy.concatenate() to join a sequence of arrays along an existing axis. The concatenate() method will take a sequence of arrays as parameters. It will concatenate the arrays into one single array and returns the concatenated array.

Now to Add a Row to a NumPy Array, In the sequence of arrays we will pass the given array and the row to be added, The concatenate() method will return the array with the row added.

Syntax of concatenate()

numpy.concatenate((a1, a2, ...), axis=0)

Parameters:

(a1, a2, ...) = Sequence of arrays to be passed to the function.
axis          = int, optional, Axis along which to concatenate arrays.

Return:

Returns a concatenated array.

Approach

  1. Import numpy library and create a numpy array
  2. Now pass the array and row to be added as a sequence of arrays to the concatenate method
  3. The method will return a copy of the array with the row added to it.
  4. Print the new array

Source code

import numpy as np

# creating  numpy array
arr = np.array([[1, 2, 3, 4, 5],
                [5, 4, 3, 2, 1]])

row = np.array([6, 7, 8, 9, 1])
        
# Adding row to array using concatenate()
arr = np.concatenate([arr, [row]])

# Array after adding the row.
print(arr)

OUTPUT:

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

3. Using insert() method to Add a Row to a NumPy Array

Numpy module in python, provides a function numpy.insert() to insert values along the given axis before the given index. The insert() method will take an array, index , values to be inserted as parameters. It will insert the given value just before the specified index and returns the array.

Now, to Add a Row to a NumPy Array we need to pass the array, index, row to be inserted to the insert() method. Here we are adding row at front of the array so let’s give index = 0.

Syntax of insert()

numpy.insert(arr, obj, values, axis=None)

Parameters:

arr          = The array to be passed to the function.
obj          = index at which value needs to be inserted
values       = Values or object to insert into array.
axis         = int, optional, Axis along which to insert values.

Return:

Returns array with value inserted at the specified index, in this case appended at the end of the array.

Approach

  • Import numpy library and create numpy array
  • Now pass the array, row to be inserted and index = 0, axis = 0 to the insert() method
  • That’s it , The insert() method will return a copy of the array with the row added.
  • Print the new array.

Source code

import numpy as np

# creating  numpy array
arr = np.array([[1, 2, 3, 4, 5],
                [5, 4, 3, 2, 1]])

row = np.array([6, 7, 8, 9, 1])
        
# Adding row to array using insert()
arr = np.insert(arr, 0, row, axis=0)

# Array after adding the row.
print(arr)

OUTPUT:

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

4. Using vstack() method to Add a Row to a NumPy Array

Numpy module in python, provides a function numpy.vstack() function is used to Stack arrays in sequence vertically (row-wise). i.e, concatenating into a single array. The vstack() method will take a sequence of arrays as parameters. It will stack the arrays into one single array and returns the array. The vstack is equivalent to concatenation.

Now to Add a Row to a NumPy Array, In the sequence of arrays we will pass the given array and the row to be added, The vstack() method will return the array with the row added.

Syntax of vstack()

numpy.vstack(tuple)

Parameters:

tuple = sequence of arrays to be passed to the function.

Return:

Returns The array formed by stacking the given arrays.

Approach

  • Import numpy library and create numpy array
  • Now pass the array, row to be inserted as a sequence of arrays to the vstack method
  • That’s it , The vstack() method will return a copy of the array with the row added.
  • Print the new array.

Source code

import numpy as np

# creating  numpy array
arr = np.array([[1, 2, 3, 4, 5],
                [5, 4, 3, 2, 1]])

row = np.array([6,7,8,9,1])

# Adding row to array using vstack()
arr = np.vstack((arr,row))

# Array after adding the row.
print(arr)

OUTPUT:

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

Summary

Great! you made it, We have discussed all possible methods to Add a Row to a NumPy Array. Happy learning.

Leave a Comment

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

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

Scroll to Top