Home > Blockchain >  Return rows that have the minimum amount of missing values pandas
Return rows that have the minimum amount of missing values pandas

Time:01-18

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

  • Row 1 has 3 missing values
  • Row 2 has 2 missing values
  • Row 3 has 8 missing values
  • Row 4 has 5 missing values
  • Row 5 has 2 missing values

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

CodePudding user response:

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

CodePudding user response:

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

df[df.isna().sum(1).eq(2)]
  •  Tags:  
  • Related