IKH

Chepter-15

How to delete elements from ndarray

We can delete elements of ndarray by using delete() function.

delete(arr, obj, axis=None)

  • obj can be int, array of ints or slice
  • for multi-dimensional arrays we must have to specify the axis, other-wise the
    default axis=None will be considered. In this case first the array is flatten to the 1-D
    array and deletion will be performed

Example

Python
In [394]: 
import numpy as np 
help(np.delete) 

Output

PowerShell
Help on function delete in module numpy: 
 
delete(arr, obj, axis=None) 
    Return a new array with sub-arrays along an axis deleted. For a one 
    dimensional array, this returns those entries not returned by 
    `arr[obj]`.

1-D arrays

Python
In [395]: 
# To delete a single element of 1-D array at a specified index 
a = np.arange(10,101,10) 
b = np.delete(a,3) # to delete the element present at 3rd index 
print(f"array a : {a}") 
print(f"array b : {b}") 

Output

PowerShell
array a : [ 10  20  30  40  50  60  70  80  90 100] 
array b : [ 10  20  30  50  60  70  80  90 100] 

Example

Python
In [396]: 
# To delete elements of 1-D array at a specified indices 
a = np.arange(10,101,10) 
b = np.delete(a,[0,4,6]) # to delete elements present at indices:0,4,6 
print(f"array a : {a}") 
print(f"array b : {b}")

Output

PowerShell
array a : [ 10  20  30  40  50  60  70  80  90 100] 
array b : [ 20  30  40  60  80  90 100] 

Example

Python
In [397]: 
# To delete elements of 1-D array from a specified range using numpy np.s_[] 
a = np.arange(10,101,10) 
b = np.delete(a,np.s_[2:6]) # to delete elements from 2nd index to 5th index 
print(f"array a : {a}") 
print(f"array b : {b}") 

Output

Python
array a : [ 10  20  30  40  50  60  70  80  90 100] 
array b : [ 10  20  70  80  90 100] 

Example

Python
In [398]: 
# To delete elements of 1-D array from a specified range using python range 
# this is applicable only for 1-D arrays. 
a = np.arange(10,101,10) 
b = np.delete(a,range(2,6)) # to delete elements from 2nd index to 5th index 
print(f"array a : {a}") 
print(f"array b : {b}")

Output

PowerShell
array a : [ 10  20  30  40  50  60  70  80  90 100] 
array b : [ 10  20  70  80  90 100]6

2-D arrays

  • Here we have to provide axis.
  • If we are not specifying access then array will be flatten(1-D) and then deletion will
    be happend.

Example

Python
In [399]: 
# without providing the axis. defautl axis=None will be taken 
a = np.arange(1,13).reshape(4,3) 
b = np.delete(a,1) 
print(f"array a : \n {a}") 
print(f"array b : \n {b}")

Output

PowerShell
array a :  
 [[ 1  2  3] 
 [ 4  5  6] 
 [ 7  8  9] 
 [10 11 12]] 
array b :  
 [ 1  3  4  5  6  7  8  9 10 11 12]

Example

Python
In [400]: 
# axis=0. deleting the specifie row 
a = np.arange(1,13).reshape(4,3) 
b = np.delete(a,1,axis=0) # row at index 1 will be deleted 
print(f"array a : \n {a}") 
print(f"array b : \n {b}")

Output

PowerShell
array a :  
 [[ 1  2  3] 
 [ 4  5  6] 
 [ 7  8  9] 
 [10 11 12]] 
array b :  
 [[ 1  2  3] 
 [ 7  8  9] 
 [10 11 12]] 

Example

Python
In [401]: 
# axis=0. deleting the specified rows 
a = np.arange(1,13).reshape(4,3) 
b = np.delete(a,[1,3],axis=0) # row at index 1 and index 3 will be deleted 
print(f"array a : \n {a}") 
print(f"array b : \n {b}") 

Output

PowerShell
array a :  
 [[ 1  2  3] 
 [ 4  5  6] 
 [ 7  8  9] 
 [10 11 12]] 
array b :  
 [[1 2 3] 
 [7 8 9]] 

Example

Python
In [402]: 
# axis=0. deleting the specified range of rows 
a = np.arange(1,13).reshape(4,3) 
b = np.delete(a,np.s_[0:3],axis=0) # rows from index-0 to index-(3-1) will be deleted 
print(f"array a : \n {a}") 
print(f"array b : \n {b}") 

Output

PowerShell
array a :  
 [[ 1  2  3] 
 [ 4  5  6] 
 [ 7  8  9] 
 [10 11 12]] 
array b :  
 [[10 11 12]] 

Example

Python
In [403]: 
# axis=0. deleting the specified range of rows with step value 
a = np.arange(1,13).reshape(4,3) 
b =  np.delete(a,np.s_[::2],axis=0) # to delete every 2nd row (alternative row) from 
index-0 
print(f"array a : \n {a}") 
print(f"array b : \n {b}") 

Output

PowerShell
array a :  
 [[ 1  2  3] 
 [ 4  5  6] 
 [ 7  8  9] 
 [10 11 12]] 
array b :  
 [[ 4  5  6] 
 [10 11 12]]

Example

Python
In [404]: 
# axis=1. deleting the specifie column 
a = np.arange(1,13).reshape(4,3) 
b = np.delete(a,1,axis=1) # column at index 1 will be deleted 
print(f"array a : \n {a}") 
print(f"array b : \n {b}") 

Output

PowerShell
array a :  
 [[ 1  2  3] 
 [ 4  5  6] 
 [ 7  8  9] 
 [10 11 12]] 
array b :  
 [[ 1  3] 
 [ 4  6] 
 [ 7  9] 
 [10 12]] 

Example

Python
In [405]: 
# axis=1. deleting the specified colunns 
a = np.arange(1,13).reshape(4,3) 
b = np.delete(a,[1,2],axis=1) # columns at index 1 and index 2 will be deleted 
print(f"array a : \n {a}") 
print(f"array b : \n {b}")

Output

PowerShell
array a :  
 [[ 1  2  3] 
 [ 4  5  6] 
 [ 7  8  9] 
 [10 11 12]] 
array b :  
 [[ 1] 
 [ 4] 
 [ 7] 
 [10]]

Example

Python
In [406]: 
# axis=1. deleting the specified range of columns 
a = np.arange(1,13).reshape(4,3) 
b = np.delete(a,np.s_[0:2],axis=1) # rows from index-0 to index-(2-1) will be deleted 
print(f"array a : \n {a}") 
print(f"array b : \n {b}") 

Output

PowerShell
array a :  
 [[ 1  2  3] 
 [ 4  5  6] 
[ 7  8  9] 
 [10 11 12]] 
array b :  
 [[ 3] 
 [ 6] 
 [ 9] 
 [12]] 

3-D arrays

Example

Python
In [407]: 
# axis= None 
a = np.arange(24).reshape(2,3,4) 
b = np.delete(a,3) # flatten to 1-D array and element at 3rd index will be deleted 
print(f"array a : \n {a}") 
print(f"array b : \n {b}") 

Output

PowerShell
array a :  
 [[[ 0  1  2  3] 
  [ 4  5  6  7] 
  [ 8  9 10 11]] 
 
 [[12 13 14 15] 
  [16 17 18 19] 
  [20 21 22 23]]] 
array b :  
 [ 0  1  2  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23] 

Example

Python
In [408]: 
# axis=0. Delete a 2-D array at the specified index 
a = np.arange(24).reshape(2,3,4) 
b = np.delete(a,0,axis=0) # 2-D array at index 0 will be deleted 
print(f"array a : \n {a}") 
print(f"array b : \n {b}") 

Output

PowerShell
array a :  
 [[[ 0  1  2  3] 
  [ 4  5  6  7] 
  [ 8  9 10 11]] 
 
 [[12 13 14 15] 
  [16 17 18 19] 
 [20 21 22 23]]] 
array b :  
 [[[12 13 14 15] 
  [16 17 18 19] 
  [20 21 22 23]]] 

Example

Python
In [409]: 
# axis=1. Delete a row at the specified index from every 2-D array. 
a = np.arange(24).reshape(2,3,4) 
b = np.delete(a,0,axis=1) # Row at index 0 will be deleted from every 2-D array 
print(f"array a : \n {a}") 
print(f"array b : \n {b}") 

Output

PowerShell
array a :  
 [[[ 0  1  2  3] 
  [ 4  5  6  7] 
  [ 8  9 10 11]] 
 
 [[12 13 14 15] 
  [16 17 18 19] 
  [20 21 22 23]]] 
array b :  
 [[[ 4  5  6  7] 
  [ 8  9 10 11]] 
 
 [[16 17 18 19] 
  [20 21 22 23]]] 

Example

Python
In [410]: 
# axis=1. Delete a column at the specified index from every 2-D array. 
a = np.arange(24).reshape(2,3,4) 
b = np.delete(a,1,axis=2) # column at index 1 will be deleted from every 2-D array 
print(f"array a : \n {a}") 
print(f"array b : \n {b}") 

Output

PowerShell
array a :  
 [[[ 0  1  2  3] 
  [ 4  5  6  7] 
  [ 8  9 10 11]] 
 [[12 13 14 15] 
  [16 17 18 19] 
  [20 21 22 23]]]
  array b :  
 [[[ 0  2  3] 
  [ 4  6  7] 
  [ 8 10 11]] 
 
 [[12 14 15] 
  [16 18 19] 
  [20 22 23]]] 

case study

Consider the following array
PowerShell
array([[ 0,  1,  2], [ 3,  4,  5], [ 6,  7,  8], [ 9, 10, 11]]) 
Delete last row and insert following row in that place:==> [70,80,90] 

Example

Python
In [411]: 
# Solution for the case study 
 
# Step:1 ==> create the required ndarray 
a = np.arange(12).reshape(4,3) 
 
# Step:2 ==> Delete the last row.  
# We have to mention the axis as axis=0 for rows. 
# obj value we can  take as -1 , which represents the last row 
b = np.delete(a,-1,axis=0) 
print("Original Array") 
print(f"array a : \n {a}") 
print("After deletion the array ") 
print(f"array b : \n {b}") 
 
# Step:3 ==> we have to insert the row [70.80.90] at the end 
# for this we can use append() function with axis=0 
c = np.append(b,[[70,80,90]],axis=0) 
print("After insertion the array ") 
print(f"array c : \n {c}") 

Output

PowerShell
Original Array 
array a : 
 [[ 0  1  2] 
 [ 3  4  5] 
 [ 6  7  8] 
 [ 9 10 11]] 
After deletion the array  
array b :  
 [[0 1 2] 
 [3 4 5] 
 [6 7 8]] 
After insertion the array  
array c :  
 [[ 0  1  2] 
 [ 3  4  5] 
 [ 6  7  8] 
 [70 80 90]]

Summary

  • insert() ==> Insert elements into an array at specified index.
  • append() ==> Append elements at the end of an array.
  • delete() ==> Delete elements from an array.

Report an error