Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep only rows that have at least one null

I am trying to do basically the opposite of drop_nulls(). I want to keep all rows that have at least one null.

I want to do something like (but I don't want to list all other columns):

for (name,) in (
    df.filter(
        pl.col("a").is_null()
        | pl.col("b").is_null()
        | pl.col("c").is_null()
    )
    .select("name")
    .unique()
    .rows()
):
    print(
        f"Ignoring `{name}` because it has at least one null",
        file=sys.stderr,
    )
df = df.drop_nulls()
like image 674
DJDuque Avatar asked Oct 28 '25 10:10

DJDuque


1 Answers

It sounds like you are looking for pl.Expr.any_horizontal. The following will keep all rows containing at least one null value (in any of the columns).

df.filter(pl.any_horizontal(pl.all().is_null()))
like image 156
Hericks Avatar answered Oct 31 '25 00:10

Hericks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!