How to delete multiple rows of NumPy array ? - GeeksforGeeks

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 :-
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 arrayx = 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 arrayx = 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 arrayx = geek.arange(35).reshape(7, 5)  # Removing all rows before the 4th rowresult = 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 arrayx = geek.arange(35).reshape(7, 5)    # Fancy indexingprint(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 arrayx = geek.arange(35).reshape(7, 5)    # applying numpy.take() functionprint(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 arrayx = geek.arange(35).reshape(7, 5)  # Performing Boolean Indexingmask_array = x[:, 0] < 10print(x[mask_array])
Output:
[[0 1 2 3 4]
 [5 6 7 8 9]]
Picked
Pyhton numpy-arrayCreation
Python numpy-Basics
Python numpy-Indexing
Python numpy-ndarray
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Check if element exists in list in Python
Selecting rows in pandas DataFrame based on conditions
Python | os.path.join() method
Defaultdict in Python
Create a directory in Python
Python | Get unique values from a list
Python | Pandas dataframe.groupby()