This article describes how to extract or delete elements, rows, and columns that satisfy the condition from the NumPy array ndarray
.
- Extract elements that satisfy the conditions
- Extract rows and columns that satisfy the conditions
- All elements satisfy the condition:
numpy.all[]
- At least one element satisfies the condition:
numpy.any[]
- All elements satisfy the condition:
- Delete elements, rows, and columns that satisfy the conditions
- Use
~
[NOT] - Use
numpy.delete[]
andnumpy.where[]
- Use
- Multiple conditions
See the following article for an example when ndarray
contains missing values NaN
.
- NumPy: Remove rows/columns with missing value [NaN] in ndarray
If you want to replace or count an element that satisfies the conditions, see the following article.
- numpy.where[]: Manipulate elements depending on conditions
- NumPy: Count the number of elements satisfying the condition
If you want to extract elements that meet the condition, you can use ndarray[conditional expression]
.
Even if the original ndarray
is a multidimensional array, a flattened one-dimensional array is returned.
import numpy as np
a = np.arange[12].reshape[[3, 4]]
print[a]
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
print[a