Open In App

How to delete multiple rows of NumPy array ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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 given below :-

  • 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 numpy.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]]


Last Updated : 09 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads