How to delete multiple rows of NumPy array ?
Last Updated :
09 Jan, 2023
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]]
Similar Reads
How to delete last N rows from Numpy array? In this article, we will discuss how to delete the last N rows from the NumPy array. Method 1: Using Slice Operator Slicing is an indexing operation that is used to iterate over an array. Â Syntax: array_name[start:stop] where start is the start is the index and stop is the last index. We can also do
4 min read
How to access different rows of a multidimensional NumPy array? Let us see how to access different rows of a multidimensional array in NumPy. Sometimes we need to access different rows of multidimensional NumPy array-like first row, the last two rows, and even the middle two rows, etc. In NumPy , it is very easy to access any rows of a multidimensional array. Al
3 min read
How to remove rows from a Numpy array based on multiple conditions ? In this article, we will learn how to remove rows from a NumPy array based on multiple conditions. For doing our task, we will need some inbuilt methods provided by the NumPy module which are as follows: np.delete(ndarray, index, axis): Delete items of rows or columns from the NumPy array based on g
3 min read
How to skip every Nth index of NumPy array ? NumPy arrays offer efficient numerical operations and data storage. When working with large arrays, sometimes it's necessary to skip specific indices for optimization or data processing purposes. This article will show how to skip every Nth index of the NumPy array. There are various ways to access
4 min read
How to Convert NumPy Matrix to Array In NumPy, a matrix is essentially a two-dimensional NumPy array with a special subclass. In this article, we will see how we can convert NumPy Matrix to Array. Also, we will see different ways to convert NumPy Matrix to Array. Convert Python NumPy Matrix to an ArrayBelow are the ways by which we can
3 min read