How to get the indices list of all NaN value in numpy array?

xxx222 picture xxx222 · Jun 10, 2016 · Viewed 110k times · Source

Say now I have a numpy array which is defined as,

[[1,2,3,4],
[2,3,NaN,5],
[NaN,5,2,3]]

Now I want to have a list that contains all the indices of the missing values, which is [(1,2),(2,0)] at this case.

Is there any way I can do that?

Answer

michael_j_ward picture michael_j_ward · Jun 10, 2016

np.isnan combined with np.argwhere

x = np.array([[1,2,3,4],
              [2,3,np.nan,5],
              [np.nan,5,2,3]])
np.argwhere(np.isnan(x))

output:

array([[1, 2],
       [2, 0]])