Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to delete multiple rows of NumPy array ?

  • Last Updated : 21 Apr, 2021

NumPy is the Python library that is used for working with arrays. In Python there are lists which serve the purpose of arrays but they are slow. Therefore, NumPy is there to provide us with the array object that is much faster than the traditional Python lists. The reason for them being faster is that they store arrays at one continuous place in memory, unlike lists which makes the process accessing and manipulation much more efficient.

There are a number of ways to delete multiple rows in NumPy array. They are give below :-

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

  • numpy.delete() – The numpy.delete() is a function in Python which returns a new array with the deletion of sub-arrays along with the mentioned axis. By keeping the value of the axis as zero, there are two possible ways to delete multiple rows using numphy.delete().
     

Using arrays of ints, Syntax: np.delete(x, [ 0, 2, 3], axis=0)

Python3






import numpy as geek
  
# Defining the array
x = geek.arange(35).reshape(7, 5
  
print(geek.delete(x, [0, 1, 2], axis=0))

Output:

[[15 16 17 18 19]                                                                                                
[20 21 22 23 24]                                                                                                
[25 26 27 28 29]          
[30 31 32 33 34]]

Using slice objects – The slice() function allows us to specify how to slice a sequence. 

Syntax of slice function: slice(start, stop, step index)

Python3




import numpy as geek
  
# Defining the array
x = geek.arange(35).reshape(7, 5)
  
print(geek.delete(x, slice(0, 3), axis=0))

Output: 

[[15 16 17 18 19]
 [20 21 22 23 24]
 [25 26 27 28 29]
 [30 31 32 33 34]]                                                                                              
  • Basic Indexing – This is one of the easiest ways to delete multiple rows of NumPy array.
    Syntax for basic indexing: array object(start:stop:step)
     

Python3




import numpy as geek
  
# Defining the array
x = geek.arange(35).reshape(7, 5)
  
# Removing all rows before the 4th row
result = x[4:]
print(result)

Output:

[[20 21 22 23 24]                                                                                                
[25 26 27 28 29]                                                                                                
[30 31 32 33 34]]   
  • Fancy Indexing – This is the method in which we index the arrays using arrays allowing us to access multiple array elements at once by referring to their index number.
    Syntax: array object[row numbers] 

Python3




import numpy as geek
  
# Defining the array
x = geek.arange(35).reshape(7, 5)  
  
# Fancy indexing
print(x[[0, 1, 2]])  

Output:

[[ 0  1  2  3  4]                                                                                                
[ 5  6  7  8  9]                                                                                               
[10 11 12 13 14]]
  • numpy.take() – The numpy.take() is a function in python which is used to return elements from arrays along the mentioned axis and indices. Now, if one mentions the value of axis as null/zero, then it is able to provide us with the desired indices and work in a way similar to Fancy Indexing.

Python3




import numpy as geek
  
# Defining the array
x = geek.arange(35).reshape(7, 5)  
  
# applying numpy.take() function
print(geek.take(x, [0, 2, 6], axis=0)) 

Output:

[[ 0  1  2  3  4]                                                                                                
[10 11 12 13 14]                                                                                                
[30 31 32 33 34]] 
  • Boolean Indexing – This is a very convenient method, specially when we put some condition for the deletion.
    For example, we want to remove the rows that start with a value greater than 10. Then we can do it by using Boolean Indexing. 

Python3




import numpy as geek
  
# Defining the array
x = geek.arange(35).reshape(7, 5)
  
# Performing Boolean Indexing
mask_array = x[:, 0] < 10
print(x[mask_array])

Output:

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :