How do I obtain the index list in a NumPy Array of all the NaN values present using Python?

0 votes

Hi all, pretty simple question but I am held back.

Let us consider the case where I have created a NumPy array which is valid. 

Consider the example:

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

And now, let us say that I want to be presented with a certain list that consists of all of the index values of the missing elements. In this case, the missing elements is [(1,2),(2,0)] as you can see.

How can I go about doing this? 

Appreciate all the help!

Jan 30 in Python by Anirudh
• 2,070 points
2,205 views

1 answer to this question.

0 votes

Hi, it is pretty simple, to be honest. Check this out:

You will have to make use of np.isnan along with no.argwhere to achieve what you desire in this question.

The following code will help give you some clarity:

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

The output for the above code is as follows:

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

I am sure this helped to answer your query, cheers!

answered Jan 30 by Nymeria
• 3,520 points

Related Questions In Python

0 votes
1 answer

how do I check the length of an array in a python program?

lets say we have a list mylist = ...READ MORE

answered Mar 12 in Python by Mohammad
• 3,170 points
76 views
0 votes
1 answer

How can I get the count of a list in Python?

len() >>> mylist=[] >>> print len(mylist) 0 READ MORE

answered Dec 12, 2018 in Python by charlie_brown
• 7,720 points
107 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Doubt in numpy.vstack

The vstack function in numpy will stack ...READ MORE

answered Jul 30 in Python by Esha
64 views
+1 vote
2 answers

View onto a numpy array?

 just index it as you normally would. ...READ MORE

answered Oct 18, 2018 in Python by roberto
59 views
0 votes
1 answer

In NumPy how do I get the maximum of subsets? Python

You can use np.maximum.reduceat: >>> _, idx = np.unique(g, ...READ MORE

answered Nov 9, 2018 in Python by Nymeria
• 3,520 points
88 views