user2487726
user2487726

Reputation:

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

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.")

Upvotes: 76

Views: 138159

Answers (6)

agravaine
agravaine

Reputation: 21

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

Upvotes: 2

Ahmed Khater
Ahmed Khater

Reputation: 7

You Can Use the code in this way

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

Upvotes: -1

João Vitor Gomes
João Vitor Gomes

Reputation: 363

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

Upvotes: 2

Collins Kelechi
Collins Kelechi

Reputation: 255

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

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

Upvotes: 4

Ikay
Ikay

Reputation: 1

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

Upvotes: -3

metersk
metersk

Reputation: 12529

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

Upvotes: 143

Related Questions