How to Drop Rows with NaN in a Pandas DataFrame

Published Dec 2, 2021  ∙  Updated May 2, 2022

How can we remove rows of a Pandas DataFrame whose value of a specific column is NaN?

Suppose we have a DataFrame df with columns A, B, and C.

Drop rows with dropna()

The most useful approach is to use dropna() to drop rows with NaN.

# Drop all rows that have any columns with NaN
df.dropna()
# Drop row if all columns are NaN
df.dropna(how='all')
# Drop row if any columns are NaN
df.dropna(how='any')
# Drop row if it has fewer than 2 non-NaN values
df.dropna(thresh=2)
# Drop row if value is NaN in specified columns
df.dropna(subset = ['A', 'B'])

I’ve found it useful to use inplace=True with dropna().

df.dropna(subset = ['A', 'B'], inplace=True)

Drop rows with notna()

We can frame the solution as a filtering problem and just use notna() for the DataFrame filtering logic.

df = df[df['C'].notna()]