Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find index of all rows with null values in a particular column in pandas dataframe

Tags:

python

pandas

I have been worried about how to find indices of all rows with null values in a particular column of a pandas dataframe in python. If A is one of the entries in df.columns then I need to find indices of each row with null values in A

like image 534
NoSuchUserException Avatar asked Jul 02 '17 09:07

NoSuchUserException


People also ask

How do you find the index of a value in a DataFrame column?

The get_loc() function is used to find the index of any column in the Python pandas dataframe. We simply pass the column name to get_loc() function to find index.

How do I get null records in pandas?

In order to check missing values in Pandas DataFrame, we use a function isnull() and notnull(). Both function help in checking whether a value is NaN or not. These function can also be used in Pandas Series in order to find null values in a series.


2 Answers

Supposing you need the indices as a list, one option would be:

df[df['A'].isnull()].index.tolist()
like image 163
Adrien Matissart Avatar answered Oct 05 '22 03:10

Adrien Matissart


np.where(df['column_name'].isnull())[0]

np.where(Series_object) returns the indices of True occurrences in the column. So, you will be getting the indices where isnull() returned True.

The [0] is needed because np.where returns a tuple and you need to access the first element of the tuple to get the array of indices.

Similarly, if you want to get the indices of all non-null values in the column, you can run

np.where(df['column_name'].isnull() == False)[0]
like image 24
vipulnj Avatar answered Oct 05 '22 01:10

vipulnj