How to remove rows that contains only NaN values in all columns of dataframe?

Viewed 4939

I have dataframe like below.

Input

df

A     B     C
1     2     1
NaN   4     2
3     NaN   NaN
NaN   NaN   NaN
4     2     NaN
NaN   NaN   NaN

Output

  A     B     C
  1     2     1
  NaN   4     2
  3     NaN   NaN
  4     2     NaN

How can this be done in python

3 Answers

You can select the df which is not NaN rather than dropping it:

df = df[~((df['A'].isna()) & (df['B'].isna()) & (df['C'].isna()))]

This gives a bit more capability if you want to filter your df by certain values of each column.

A     B     C
1     2     1
NaN   4     2
3     NaN   NaN
4     2     NaN

You can use df.dropna?? to get the information about the dropna functionality.

Related