Skip to content

Drop all rows that contain null values

Description

The original order of the remaining rows is preserved.

Usage

<DataFrame>$drop_nulls(...)

Arguments

\<dynamic-dots\> Column name(s) for which null values are considered. If empty (default), use all columns.

Value

A polars DataFrame

Examples

library("polars")

df <- pl$DataFrame(
  foo = 1:3,
  bar = c(6L, NA, 8L),
  ham = c("a", "b", NA)
)

# The default behavior of this method is to drop rows where any single value
# of the row is null.
df$drop_nulls()
#> shape: (1, 3)
#> ┌─────┬─────┬─────┐
#> │ foo ┆ bar ┆ ham │
#> │ --- ┆ --- ┆ --- │
#> │ i32 ┆ i32 ┆ str │
#> ╞═════╪═════╪═════╡
#> │ 1   ┆ 6   ┆ a   │
#> └─────┴─────┴─────┘
# This behaviour can be constrained to consider only a subset of columns, as
# defined by name or with a selector. For example, dropping rows if there is
# a null in any of the integer columns:
df$drop_nulls(cs$integer())
#> shape: (2, 3)
#> ┌─────┬─────┬──────┐
#> │ foo ┆ bar ┆ ham  │
#> │ --- ┆ --- ┆ ---  │
#> │ i32 ┆ i32 ┆ str  │
#> ╞═════╪═════╪══════╡
#> │ 1   ┆ 6   ┆ a    │
#> │ 3   ┆ 8   ┆ null │
#> └─────┴─────┴──────┘