It is common for the NumPy arrays to contain NA values. Let’s see how we can get the index of the NA values in NumPy arrays efficiently. Assume that we are dealing with the following NumPy array.
import numpy as np my_arr = [[1,2,np.nan], [4,np.nan,6], [np.nan,8,9]] my_arr
[[1, 2, nan], [4, nan, 6], [nan, 8, 9]]
We can use the argwhere
and is.nan
methods to get the location of the NAs. For example:
np.argwhere(np.isnan(my_arr))
array([[0, 2],
[1, 1],
[2, 0]], dtype=int64)
As we can see, we got the location of the NA values.