Search code examples
pythondataframeconditional-statementspython-polarsboolean-logic

Check if all values of DataFrame are True


How can I check if all values of a polars DataFrame, containing only boolean columns, are True?
Example df:

df = pl.DataFrame({"a": [True, True, None],
                   "b": [True, True, True],
    })

The reason for my question is that sometimes I want to check if all values of a df fulfill a condition, like in the following:

df = pl.DataFrame({"a": [1, 2, None],
                   "b": [4, 5, 6],
}).select(pl.all() >= 1)

By the way, I didn't expect that .select(pl.all() >= 1) keeps the null (None) in last row of column "a", maybe that's worth noting.


Solution

  • As of the date of this answer, I found the following snippet most appropriate for polars:

    df.fill_null(False).min_horizontal().min()  
    

    If no null values exist in df, one could omit .fill_null(False).

    Credit goes to roman, the logic of min_horizotnal().min() was first described by him in this answer on a similar issue on any.

    Example with the df from above:

    >>> df.fill_null(False).min_horizontal().min()
    False
    >>> df = pl.DataFrame({"a": [True, True, True],
    ...                    "b": [True, True, True],
    ...     })
    ...
    ... df.fill_null(False).min_horizontal().min()
    True