Python | Ways to add row/columns in numpy array
Last Updated :
22 Mar, 2023
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
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 (arr))
|
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]]
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 (arr))
|
Output:
resultant array [[ 1 2 3 1]
[45 4 7 2]
[ 9 6 10 3]]
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 (arr))
|
Output:
resultant array [[ 1 2 3 1]
[45 4 7 2]
[ 9 6 10 3]]
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]]
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]]
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 (arr))
|
Output:
resultant array [[ 1 2 3]
[45 4 7]
[ 9 6 10]
[ 1 2 3]]
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]]
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]]
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...