dropna
df.dropna() removes every row that has at least one NaN; subset=["col"] limits the check to those columns and how="all" drops only rows where every value is missing. axis=1 drops columns instead of rows.
Syntax
df.dropna(axis=0, how="any", thresh=None, subset=None, inplace=False, ignore_index=False)| Part | What it is |
|---|---|
axis | 0 drops rows (default), 1 drops columns |
how | "any" drops when any value is NaN, "all" only when all are NaN |
thresh | Keep rows with at least this many non-NaN values |
subset | Column names to check (row labels when axis=1) |
Examples
import pandas as pd
import numpy as np
df = pd.DataFrame({"id": [1, 2, 3, 4],
"email": ["a@x.com", None, "c@x.com", None],
"score": [8.5, 7.0, np.nan, np.nan]})
df.dropna()Result: id email score 0 1 a@x.com 8.5
df.dropna(subset=["email"])Result: id email score 0 1 a@x.com 8.5 2 3 c@x.com NaN
Only rows missing an email are removed.
df.dropna(thresh=2)Result: id email score 0 1 a@x.com 8.5 1 2 None 7.0 2 3 c@x.com NaN
Row 3 has only one non-null value (id) and is dropped.
Common mistakes
- Almost all rows disappear: the default checks every column, so one sparse column wipes the table; use subset to check the columns that matter.
- The frame is unchanged: dropna returns a new DataFrame; assign it or use inplace=True.
- Empty strings or the text "null" are not NaN and survive dropna; convert them with replace or na_values in read_csv first.
Practice dropna
These drills check your answer on live data, free, no account needed.
- Drop rows missing a key fieldDrop the contacts that have no email. Contacts without a phone number stay. Keep the original row labels.
Questions
How do I drop columns that are completely empty?
df.dropna(axis=1, how="all") removes columns where every value is NaN.
How do I drop rows where a specific column is NaN?
df.dropna(subset=["col"]), or equivalently df[df["col"].notna()].
How do I count rows with any missing value?
df.isna().any(axis=1).sum() counts rows with at least one NaN.
Related
- fillnaHow to fill NaN values in pandas with fillna: a constant, a different value per column, the column mean or median, forward and backward fill, and by group.
- drop_duplicatesHow to remove duplicates in pandas with drop_duplicates: whole rows or a subset of columns, keep first, last or none, and finding duplicates with duplicated.
- Filter rowsHow to filter rows in a pandas DataFrame by one or several conditions using boolean masks, with the & | ~ operators, parentheses, isin, between and query.
- describeWhat df.describe returns in pandas: count, mean, std, min, quartiles and max for numeric columns, including text columns, custom percentiles and transposing.