Deya
Deya

Reputation: 79

How to delete each row having all columns is equal to None for a data frame

For example data frame example I want to delete each row which are having all columns = NA. I don't want to delete rows which are not having NAs in all columns. Kindly give some suggestions with examples. I really appreciate your help.

Upvotes: 1

Views: 1336

Answers (3)

surya rahul
surya rahul

Reputation: 883

You can specify the column names in dropna as:

df.dropna(subset = ['column1_name', 'column2_name', 'column3_name'])

This will remove the NA values.

Upvotes: 0

niraj
niraj

Reputation: 18218

You can try:

df.dropna(how='all', inplace=True)

From documentation (pandas.DataFrame.dropna):

how : {‘any’, ‘all’}, default ‘any’

Determine if row or column is removed from DataFrame, when we have at least one NA or all NA.

‘any’ : If any NA values are present, drop that row or column.

‘all’ : If all values are NA, drop that row or column.

and inplace:

inplace : bool, default False

If True, do operation inplace and return None.

Upvotes: 3

loegare
loegare

Reputation: 152

youre looking for pd.dropna(axis=0,thresh=3)

if you set thresh = to the number of columns you have itll only drop rows with everything nan.

docs

edit: i misread how=all, students comment is probably a better anwser

Upvotes: 1

Related Questions