Remove incomplete rows
Prepare complete-case data before analysis when any missing field makes a row unusable.
Polars · Module
Removes rows containing nulls from a Polars DataFrame. By default every column is checked, but subset can restrict the check to named columns or a selector.
drop_nulls returns a new DataFrame after removing rows that have nulls in the selected columns. With no subset, every column participates; with a column name collection or selector, only those columns determine which rows are removed, and the surviving rows keep their original order.
DataFrame.drop_nulls(subset: ColumnNameOrSelector | Collection[ColumnNameOrSelector] | None = None) → DataFrame| Name | Type | Description |
|---|---|---|
subset | ColumnNameOrSelector | Collection[ColumnNameOrSelector] | None = None | Column name or names whose null values are considered; when None, all columns are used. A selector is also accepted. |
A new DataFrame with rows containing considered null values removed.
subset unspecified makes every DataFrame column participate in the null check.subset limits the columns whose nulls can cause row removal.drop_nans() when NaN handling is required.import polars as pl
import polars.selectors as cs
df = pl.DataFrame({
"id": [1, 2, 3],
"score": [10, None, 30],
"label": ["a", "b", None],
})
clean = df.drop_nulls(subset=cs.numeric())
print(clean)Removes rows with nulls in numeric columns while retaining the row whose only null is in the non-numeric label column.
Prepare complete-case data before analysis when any missing field makes a row unusable.
Validate required fields while allowing optional columns to remain null.
Apply the same missingness rule across a type-defined group of columns.
DataFrame.drop_nulls() do by default?It returns a DataFrame containing only rows without nulls in the columns being considered. If subset is omitted, all columns are checked, and the remaining rows preserve their original order.
Follow-up: How would you retain rows with nulls in nonessential columns? Pass only the required column names or a selector through subset, so nulls elsewhere do not trigger removal.
subset parameter change behavior?subset identifies the columns whose null values should cause a row to be dropped. It accepts one column name, a collection of column names, or a selector; None means all columns.
Follow-up: How could you apply the same rule to every integer column without listing them? Use a selector such as polars.selectors.integer() as the subset.
drop_nulls remove NaN values?The operation checks for null values, not NaN values. A floating-point NaN therefore requires the separate drop_nans() method if it should be removed.
Follow-up: What should be used when both missing-value representations may occur? Apply the appropriate null and NaN operations explicitly, because the documentation distinguishes the two values.
DataFrame.drop_nanspolars.selectors.integer