[Python] Check if any value in a Polars DataFrame is True

Discussão em 'Python' iniciado por Stack, Abril 12, 2025.

  1. Stack

    Stack Membro Participativo

    This is quite a simple ask but I can't seem to find any clear simplistic solution to this, feels like I'm missing something.

    Let's say I have a DataFrame of type

    df = pl.from_repr("""
    ┌───────┬───────┬───────┐
    │ a ┆ b ┆ c │
    │ --- ┆ --- ┆ --- │
    │ bool ┆ bool ┆ bool │
    ╞═══════╪═══════╪═══════╡
    │ false ┆ true ┆ false │
    │ false ┆ false ┆ false │
    │ false ┆ false ┆ false │
    └───────┴───────┴───────┘
    """)


    How do I do a simple check if any of the values in the DataFrame is True? Some solutions I have found is

    selection = df.select(pl.all().any(ignore_nulls=True))


    or

    selection = df.filter(pl.any_horizontal())


    and then check in that row

    any(selection.row(0))


    Is just seems like so many steps for a single check

    Continue reading...

Compartilhe esta Página