How do i add elements to a row in a matrix in python?

As this question is been 7 years before, in the latest version which I am using is numpy version 1.13, and python3, I am doing the same thing with adding a row to a matrix, remember to put a double bracket to the second argument, otherwise, it will raise dimension error.

In here I am adding on matrix A

1 2 3
4 5 6

with a row

7 8 9

same usage in np.r_

A = [[1, 2, 3], [4, 5, 6]]
np.append(A, [[7, 8, 9]], axis=0)

    >> array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])
#or 
np.r_[A,[[7,8,9]]]

Just to someone's intersted, if you would like to add a column,

array = np.c_[A,np.zeros(#A's row size)]

following what we did before on matrix A, adding a column to it

np.c_[A, [2,8]]

>> array([[1, 2, 3, 2],
          [4, 5, 6, 8]])

If you want to prepend, you can just flip the order of the arguments, i.e.:

np.r_([[7, 8, 9]], A)

    >> array([[7, 8, 9],
             [1, 2, 3],
             [4, 5, 6]])

Given a Numpy array, the task is to add rows/columns basis on requirements to the Numpy array. Let’s see a few examples of this problem in Python.

Add columns in the Numpy array

Method 1: Using np.append()

Python3

import numpy as np

ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])

print("initial_array : ", str(ini_array));

column_to_be_added = np.array([[1], [2], [3]])

arr = np.append(ini_array, column_to_be_added, axis=1)

print ("resultant array", str(result))

Output:

initial_array :  [[ 1  2  3]
 [45  4  7]
 [ 9  6 10]]
resultant array [[ 1  2  3  1]
 [45  4  7  2]
 [ 9  6 10  3]]

Method 2: Using np.concatenate

Python3

import numpy as np

ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])

column_to_be_added = np.array([[1], [2], [3]])

arr = np.concatenate([ini_array, column_to_be_added], axis=1)

print ("resultant array", str(result))

Output:

resultant array [[ 1  2  3  1]
 [45  4  7  2]
 [ 9  6 10  3]]

Method 3: Using np.insert()

Python3

import numpy as np

ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])

column_to_be_added = np.array([[1], [2], [3]])

arr = np.insert(ini_array, 0, column_to_be_added, axis=1)

print ("resultant array", str(result))

Output:

resultant array [[ 1  2  3  1]
 [45  4  7  2]
 [ 9  6 10  3]]

Method 4: Using np.hstack()  

Python3

import numpy as np

ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])

column_to_be_added = np.array([1, 2, 3])

result = np.hstack((ini_array, np.atleast_2d(column_to_be_added).T))

print ("resultant array", str(result))

Output:

resultant array [[ 1  2  3  1]
 [45  4  7  2]
 [ 9  6 10  3]]

Method 5: Using np.column_stack() 

Python3

import numpy as np

ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])

column_to_be_added = np.array([1, 2, 3])

result = np.column_stack((ini_array, column_to_be_added))

print ("resultant array", str(result))

Output: 

resultant array [[ 1  2  3  1]
 [45  4  7  2]
 [ 9  6 10  3]]

Add row in Numpy array

Method 1: Using np.r_ 

Python3

import numpy as np

ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])

print("initial_array : ", str(ini_array));

row_to_be_added = np.array([1, 2, 3])

result = np.r_[ini_array,[row_to_be_added]]

print ("resultant array", str(result))

Output:

initial_array :  [[ 1  2  3]
 [45  4  7]
 [ 9  6 10]]

resultant array [[ 1  2  3]
 [45  4  7]
 [ 9  6 10]
 [ 1  2  3]]

Method 2: Using np.insert 

Python3

import numpy as np

ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])

row_to_be_added = np.array([1, 2, 3])

row_n = arr.shape[0]

arr = np.insert(ini_array,row_n,[row_to_be_added],axis= 0)

print ("resultant array", str(result))

Output:

resultant array [[ 1  2  3]
 [45  4  7]
 [ 9  6 10]
 [ 1  2  3]]

Method 3: Using np.vstack() 

Python3

import numpy as np

ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])

row_to_be_added = np.array([1, 2, 3])

result = np.vstack ((ini_array, row_to_be_added) )

print ("resultant array", str(result))

Output:

resultant array [[ 1  2  3]
 [45  4  7]
 [ 9  6 10]
 [ 1  2  3]]

Method 4: Using numpy.append()

Sometimes we have an empty array and we need to append rows in it. Numpy provides the function to append a row to an empty Numpy array using numpy.append() function.

Example 1: Adding new rows to an empty 2-D array

Python3

import numpy as np  

empt_array = np.empty((0,2), int)

print("Empty array:")

print(empt_array)

empt_array = np.append(empt_array, np.array([[10,20]]), axis=0)

empt_array = np.append(empt_array, np.array([[40,50]]), axis=0)

print("\nNow array is:")

print(empt_array)

Empty array:
[]

Now array is:
[[10 20]
[40 50]]

Example 2: Adding new rows to an empty 3-D array

Python3

import numpy as np  

empt_array = np.empty((0,3), int)

print("Empty array:")

print(empt_array)

empt_array = np.append(empt_array, np.array([[10,20,40]]), axis=0)

empt_array = np.append(empt_array, np.array([[40,50,55]]), axis=0)

empt_array = np.append(empt_array, np.array([[40,50,55]]), axis=0)

print("\nNow array is:")

print(empt_array)

Empty array:
[]

Now array is:
[[10 20 40]
 [40 50 55]
 [40 50 55]]

Example 3: Adding new rows to an empty 4-D array

Python3

import numpy as np  

empt_array = np.empty((0,4), int)

print("Empty array:")

print(empt_array)

empt_array = np.append(empt_array, np.array([[100,200,400,888]]), axis=0)

empt_array = np.append(empt_array, np.array([[405,500,550,558]]), axis=0)

empt_array = np.append(empt_array, np.array([[404,505,555,145]]), axis=0)

empt_array = np.append(empt_array, np.array([[44,55,550,150]]), axis=0)

print("\nNow array is:")

print(empt_array)

Empty array:
[]

Now array is:
[[100 200 400 888]
 [405 500 550 558]
 [404 505 555 145]
 [ 44  55 550 150]]

How do you add a value to a matrix in Python?

If you are using array module, you can use the concatenation using the + operator, append(), insert(), and extend() functions to add elements to the array. If you are using NumPy arrays, use the append() and insert() function.

How do I add an element to each row of a NumPy array?

Numpy provides the function to append a row to an empty Numpy array using numpy. append() function.

How do you add a row to a element in Python?

In this article, we will learn how to add a row to a 2D NumPy Array in python..
Import numpy library and create a numpy array..
Pass the array, row to be added to the append() method and set axis=0..
The append() method will return copy of the array by adding the row..
Print the new array..

Can you add elements to an array Python?

You can use the append() method to add an element to an array.