dropna() (Pandas)

dropna() returns rows or columns that meet a specified non-missing-data requirement. Use subset, how or thresh to state which observations are required before discarding records.

Pandas

By default, DataFrame.dropna() removes rows containing any missing entry. how="all" removes only completely missing rows. thresh=N requires at least N known values, and must not be passed together with how, even when one is explicitly set to None.

When dropping rows, subset names the columns to examine. For example, dropna(subset=["email"]) requires an email but does not discard a record just because an optional note is absent. With axis=1, the operation instead removes columns.

The returned table excludes records, so this is a data policy rather than harmless formatting. Capture the returned frame; ordinary reassignment changes a variable binding and does not mutate an alias of the original frame. Retain counts or missingness flags when exclusions need to be explained.

Syntax

df.dropna(subset=["required_field"])
df.dropna(how="all")
df.dropna(thresh=2)

Examples

Python

import pandas as pd
frame = pd.DataFrame({"email": ["[email protected]", None, "[email protected]"], "note": [None, "call", "ready"]})
print(frame.dropna(subset=["email"]).index.tolist())
print(frame.dropna(thresh=2).index.tolist())
try:
    frame.dropna(how="any", thresh=None)
except TypeError as error:
    print(type(error).__name__ + ": " + str(error))

Output:

[0, 2]
[2]
TypeError: You cannot set both the how and thresh arguments at the same time.

Learn it in a lesson

See also

Back to the Dictionary