kondal
kondal

Reputation: 161

How to find missing values positions in python?

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.

Upvotes: 3

Views: 4351

Answers (4)

Ivo Chula
Ivo Chula

Reputation: 211

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.

Upvotes: 1

Dulangi_Kanchana
Dulangi_Kanchana

Reputation: 1233

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

Upvotes: 1

maeVeyable
maeVeyable

Reputation: 152

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)]

Upvotes: 2

EdChum
EdChum

Reputation: 394239

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),)

Upvotes: 2

Related Questions