3

If we want to find missing values positions in a vector, we can use which and is.na functions in R.

a=c(1,2,3,NA,5,6,NA)
positions=which(is.na(a))

How can we find missing values positions in python?

a=[1,2,3,np.nan,5,6,np.nan]
positions=pd.isnull(a)

Here I can get true or false. But I want to find missing values positions.

Thanks in advance.

0

4 Answers 4

2

You can use np.nonzero:

In [307]:

a=[1,2,3,np.nan,5,6,np.nan]
np.nonzero(pd.isnull(a))
Out[307]:
(array([3, 6], dtype=int64),)
Sign up to request clarification or add additional context in comments.

Comments

2

You can use enumerate and test each element of your list whith the numpy method : isnan()

indexes = [index for index,element in enumerate(a) if np.isnan(element)]

Comments

1

when using

pd.isnull(a)

you get true as output when the value at that cell is missing, you get false as output if the cell you are referring to has a certain value, and no missing value, here in this url you can find different ways to detect missing values https://medium.com/@kanchanardj/jargon-in-python-used-in-data-science-to-laymans-language-part-two-98787cce0928

Comments

1

If you have a pandas.Dataframe you can use this approach

df.loc[df['Feature'].isnull()]

It will return the lines of the specific column on thedataframe where Nan values are present.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.