Marc
Marc

Reputation: 601

Return rows that have the minimum amount of missing values pandas

Let's say I have a table that has 5 rows and 10 columns:

I would like the function to return me row 2 & 5

Upvotes: 1

Views: 962

Answers (2)

wwnde
wwnde

Reputation: 26676

Use isna().sum equals to to generate a boolean and then subset

df[df.isna().sum(1).eq(2)]

Upvotes: 0

Marc
Marc

Reputation: 601

df.isnull().sum(axis=1) will return the number of missing values per rows.

min(df.isnull().sum(axis=1)) will return the minimum missing values for a row

df[df.isnull().sum(axis=1) == min(df.isnull().sum(axis=1))] will return the rows that have the minimum amount of missing values

Upvotes: 2

Related Questions