Python, Pandas : Return only those rows which have missing values

PythonPandasMissing Data

Python Problem Overview


While working in Pandas in Python...

I'm working with a dataset that contains some missing values, and I'd like to return a dataframe which contains only those rows which have missing data. Is there a nice way to do this?

(My current method to do this is an inefficient "look to see what index isn't in the dataframe without the missing values, then make a df out of those indices.")

Python Solutions


Solution 1 - Python

You can use any axis=1 to check for least one True per row, then filter with boolean indexing:

null_data = df[df.isnull().any(axis=1)]

Solution 2 - Python

df.isnull().any(axis = 1).sum()

this gives you the total number of rows with at least one missing data

Solution 3 - Python

If you want to see only the rows that contains the NaN values you could do:

data_frame[data_frame.iloc[:, insert column number here]=='NaN']

Solution 4 - Python

I just had this problem I assume you want to view a section of data frame made up of rows with missing values I used

df.loc[df.isnull().any(axis=1)]

Solution 5 - Python

You Can Use the code in this way

sum(df.isnull().any(axis=1))

Solution 6 - Python

If you are looking for a quicker way to find the total number of missing rows in the dataframe, you can use this:

sum(df.isnull().values.any(axis=1))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2487726View Question on Stackoverflow
Solution 1 - PythonmeterskView Answer on Stackoverflow
Solution 2 - PythonCollins KelechiView Answer on Stackoverflow
Solution 3 - PythonJoão Vitor GomesView Answer on Stackoverflow
Solution 4 - PythonagravaineView Answer on Stackoverflow
Solution 5 - PythonAhmed KhaterView Answer on Stackoverflow
Solution 6 - PythonIkayView Answer on Stackoverflow