Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Ways to add row/columns in numpy array

Python | Ways to add row/columns in numpy array

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]])
 
# printing initial array
print("initial_array : ", str(ini_array));
 
# Array to be added as column
column_to_be_added = np.array([[1], [2], [3]])
 
# Adding column to array using append() method
arr = np.append(ini_array, column_to_be_added, axis=1)
 
# printing result
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]]

Method 2: Using np.concatenate

Python3




import numpy as np
 
ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
 
# Array to be added as column
column_to_be_added = np.array([[1], [2], [3]])
 
# Adding column to array using append() method
arr = np.concatenate([ini_array, column_to_be_added], axis=1)
 
 
# printing result
print ("resultant array", str(arr))


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]])
 
# Array to be added as column
column_to_be_added = np.array([[1], [2], [3]])
 
# Adding column to array using append() method
arr = np.insert(ini_array, 0, column_to_be_added, axis=1)
 
 
# printing result
print ("resultant array", str(arr))


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]])
 
# Array to be added as column
column_to_be_added = np.array([1, 2, 3])
 
# Adding column to numpy array
result = np.hstack((ini_array, np.atleast_2d(column_to_be_added).T))
 
# printing result
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]])
 
# Array to be added as column
column_to_be_added = np.array([1, 2, 3])
 
# Adding column to numpy array
result = np.column_stack((ini_array, column_to_be_added))
 
# printing result
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]])
 
# printing initial array
print("initial_array : ", str(ini_array));
 
# Array to be added as row
row_to_be_added = np.array([1, 2, 3])
 
# Adding row to numpy array
result = np.r_[ini_array,[row_to_be_added]]
 
# printing result
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]])
 
# Array to be added as row
row_to_be_added = np.array([1, 2, 3])
 
#last row
row_n = arr.shape[0]
arr = np.insert(ini_array,row_n,[row_to_be_added],axis= 0)
 
# printing result
print ("resultant array", str(arr))


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]])
 
# Array to be added as row
row_to_be_added = np.array([1, 2, 3])
 
# Adding row to numpy array
result = np.vstack ((ini_array, row_to_be_added) )
 
# printing result
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




# importing Numpy package
import numpy as np  
 
# creating an empty 2d array of int type
empt_array = np.empty((0,2), int)
print("Empty array:")
print(empt_array)
 
# adding two new rows to empt_array
# using np.append()
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




# importing Numpy package
import numpy as np  
 
# creating an empty 3d array of int type
empt_array = np.empty((0,3), int)
print("Empty array:")
print(empt_array)
 
# adding three new rows to empt_array
# using np.append()
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




# importing Numpy package
import numpy as np  
 
# creating an empty 4d array of int type
empt_array = np.empty((0,4), int)
print("Empty array:")
print(empt_array)
 
# adding four new rows to empt_array
# using np.append()
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]]

RELATED ARTICLES

Most Popular

Recent Comments