Predictive Hacks

How to Find the Location of NA values in NumPy Arrays

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.

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.