• Post author:
  • Post category:Polars
  • Post last modified:April 24, 2025
  • Reading time:14 mins read
You are currently viewing Check if any Value in a Polars DataFrame is True

Checking if any value in a Polars DataFrame is True means determining whether at least one element in the entire DataFrame holds a True value (i.e., a boolean value that represents a logical truth).

Advertisements

In a DataFrame, values can be of various types, such as integers, floats, strings, or booleans. When you’re checking if any element is True, you’re specifically looking to see if any value evaluates to True in a boolean context across the entire DataFrame. This is often useful for logical validations or conditional operations. In this article, I will explain how to check if any value in a Polars DataFrame is True.

Key Points –

  • Use to_numpy().any() to check across the entire DataFrame after converting it to a NumPy array.
  • Use df.select(pl.all().any()) to perform a column-wise any() check across all columns.
  • Combine select(pl.all().any()) with to_series().any() to determine if any column has at least one True.
  • Use pl.all().any(ignore_nulls=True) to perform a column-wise check while ignoring null values.
  • Apply any_horizontal() to check if any value is True across each row.
  • Use max_horizontal() as an alternative row-wise any() check, leveraging True as 1.
  • Aggregate max_horizontal() with max() to determine if any row contains True.
  • Use unpivot() followed by a select(pl.col("value").any()) to check for True after reshaping the DataFrame.

Usage of Check if any Value in a Polars DataFrame is True

To check if any value in the entire Polars DataFrame is True, you can use the any() function along with to_numpy() to convert the DataFrame values into a NumPy array. This allows you to then check if any of the values in the array are True by applying the appropriate method.

To run some examples to check if any value in a polars DataFrame is True, let’s create a Polars DataFrame.


import polars as pl

# Example Polars DataFrame
df = pl.DataFrame({
    "a": [True, False, False],
    "b": [False, False, True]
})

print("Original DataFrame:\n", df)

Yields below output.

polars DataFrame Check any Value

To check if any value in the entire DataFrame is True, you can use a combination of to_numpy() and any() on the whole DataFrame, or use any() directly on the columns.


# Convert the DataFrame to a NumPy array 
# And check if any value is True
result = df.to_numpy().any()
print("Any value is True?:\n", result)

Here,

  • df.to_numpy() – This converts the Polars DataFrame into a NumPy array.
  • any() – This checks if any value in the entire array is True. Since the DataFrame has True values in both column a (index 0) and column b (index 2), the result is True.
polars DataFrame Check any Value

Using any() for a Column-Wise Check

To perform a column-wise check for any True values in each column of a Polars DataFrame, you can use the any() method in combination with pl.col() for each column.


# Check if any value is True in each column
result = df.select([pl.col(c).any().alias(c) for c in df.columns])
print(result)

# Output:
# shape: (1, 2)
┌──────┬──────┐
│ a    ┆ b    │
│ ---  ┆ ---  │
│ bool ┆ bool │
╞══════╪══════╡
│ true ┆ true │
└──────┴──────┘

Here,

  • df.select([pl.col(c).any().alias(c) for c in df.columns]) This iterates over each column in the DataFrame (df.columns), checks if there is any True value in each column using any(), and renames the result as the column’s name using alias(c).
  • The output will display True for any column that contains at least one True value. For the provided example DataFrame: Column a has a True at index 0, so a.any() is True. Column b has a True at index 2, so b.any() is True as well.

Using any_horizontal() on All Columns

To use any_horizontal() to check for any True value across all columns in each row, you can apply it to the DataFrame. This method will return True for each row where at least one value in that row is True.


# Check if any value in a row is True across all columns
result = df.select(pl.any_horizontal("*"))
print(result)

# Output:
# shape: (3, 1)
┌───────┐
│ a     │
│ ---   │
│ bool  │
╞═══════╡
│ true  │
│ false │
│ true  │
└───────┘

Here,

  • any_horizontal("*") checks horizontally (i.e., across each row) if any column in that row has a True value.
  • The "*" inside the parentheses means we apply it to all columns in the DataFrame.

Similarly, If you want to perform a row-wise any(), and then check globally if any row has at least one True,


# Step 1: Row-wise check — any True in each row
row_any = df.select(pl.any_horizontal("*")).to_series()

# Step 2: Global check — is any row True?
result = row_any.any()
print(result)

# Using any_horizontal() on all columns
df.select(pl.any_horizontal("*")).to_series().any()
print(result)

# Output:
# True

Here,

  • pl.any_horizontal("*") Checks if any column in each row is True.
  • to_series() Converts the result from a DataFrame to a Series.
  • any() Checks if any row has at least one True, i.e., is there any row that’s not entirely False?

Using any() with select() Method

Using any() with select() in Polars is a great way to check if any value is True in each column. This works column-wise, returning a DataFrame with one row where each column shows whether it contains any True values.


# Column-wise any using .select()
result = df.select(pl.all().any())
print(result)

# Column-wise any, ignoring nulls
result = df.select(pl.all().any(ignore_nulls=True))
print(result)

# Output:
# shape: (1, 2)
┌──────┬──────┐
│ a    ┆ b    │
│ ---  ┆ ---  │
│ bool ┆ bool │
╞══════╪══════╡
│ true ┆ true │
└──────┴──────┘

Here,

  • pl.all() selects all columns.
  • any() checks for any True in each column.
  • The result is a single-row DataFrame showing whether each column contains at least one True.

Using max_horizontal() to Check Across Rows

You can cleverly use max_horizontal() to perform a row-wise “any” check on boolean data. This works because True is treated as 1 and False as 0. So, if any value in a row is True, the row’s horizontal maximum will also be True.


# Row-wise any using max_horizontal, then global any
result = df.max_horizontal().max()
print(result)

# Output:
# True

Here,

  • df.max_horizontal() For each row, gets the max (i.e., True if any value is True).
  • max() Aggregates across all rows, tells you if any row had at least one True.

Using unpivot() and any()

To check if any value in the entire DataFrame is True using unpivot() and any(), you can unpivot the DataFrame, turning columns into rows, and then use any() to check across the unpivoted data.


# Unpivot the DataFrame and check if any value is True
result = df.unpivot().select(pl.col("value").any())
print(result)

# Output:
# shape: (1, 1)
┌───────┐
│ value │
│ ---   │
│ bool  │
╞═══════╡
│ true  │
└───────┘

Here,

  • df.unpivot() Converts the DataFrame from wide format (with columns) to long format (with rows). Each column gets “melted” into two columns: name (the column name) and value (the cell value).
  • select(pl.col("value").any()) Checks if there’s any True in the value column (which contains all values from the original DataFrame after unpivoting).

Conclusion

In conclusion, Polars offers powerful and efficient ways to check for True values in a DataFrame. You can use any() method to check column-wise if any value is True. For a row-wise “any” check, leveraging max_horizontal() is an effective approach, as it treats True as 1 and False as 0, ensuring that a row will return True if any value in that row is True. These methods make it easy to perform logical checks in your DataFrame while maintaining performance and clarity.

Happy Learning!!

References