# Boolean expressions and filtering
source: https://docs.chalk.ai/docs/chalkdf/expressions/filtering

## Compose reusable predicates and decide which rows to retain when values are missing.

A review queue needs positive charges and a separate rule for refunds. Start by keeping the predicates as expressions. Inspecting them alongside their inputs makes it easier to check boundary and null cases before you discard rows.

### Inspect a predicate

Compare the threshold against values exactly on the boundary, below it, and missing.

```python
from chalk import _, functions as F
from chalkdf import DataFrame

tx = DataFrame({
    "id": [1, 2, 3, 4], "kind": ["charge", "charge", "refund", "charge"],
    "amount": [10, 9, 4, None],
})

large = _.amount >= 10
tx.project({"id": _.id, "amount": _.amount, "large": large}).run()
```

| id · `int64` | amount · `int64` | large · `bool` |
| ------------ | ---------------- | -------------- |
| 1            | 10               | True           |
| 2            | 9                | False          |
| 3            | 4                | False          |
| 4            | null             | null           |

The comparison is inclusive. A missing amount produces a null predicate.

### Combine conditions

Put parentheses around each comparison before combining them with & or |. Retain a charge at or above the threshold, or any refund.

```python
needs_review = ((_.kind == "charge") & large) | (_.kind == "refund")
tx.filter(needs_review).run()
```

| id · `int64` | kind · `string` | amount · `int64` |
| ------------ | --------------- | ---------------- |
| 1            | charge          | 10               |
| 3            | refund          | 4                |

The refund qualifies independently of its amount. Filtering preserves the input columns and keeps rows whose predicate evaluates to true.

### Decide where unknowns go

If unreported amounts also require review, include that policy in the predicate.

```python
tx.filter(needs_review | F.is_null(_.amount)).select("id", "kind").run()
```

| id · `int64` | kind · `string` |
| ------------ | --------------- |
| 1            | charge          |
| 3            | refund          |
| 4            | charge          |

ID 4 now appears in the queue. Using [`F.coalesce(_.amount, 0) >= 10`](/api-docs#coalesce) would instead treat its missing amount as below the threshold.

### Filter groups after aggregation

Sum the known charges for each kind, then apply a threshold to the resulting totals.

```python
tx.filter(F.is_not_null(_.amount)).agg(
    ["kind"], _.amount.sum().alias("total")
).filter(_.total >= 15).run()
```

| kind · `string` | total · `int64` |
| --------------- | --------------- |
| charge          | 19              |

Both charges contribute to the total of 19. Applying the threshold to individual input rows would discard them before they could be added together.

### Count distinct matches

To count how many types of transaction have a qualifying amount, filter first and deduplicate the projected type. Counting the filtered rows alone would count purchases.

```python
matches = tx.filter(_.amount >= 4).select("kind")
matches.distinct_on("kind").agg([], _.count().alias("distinct_kinds")).run()
```

| distinct_kinds · `int64` |
| --- |
| 2 |

The two charge rows contribute one kind. The refund contributes a second kind.

See [missing data](/docs/chalkdf/expressions/missing-data) and [DataFrame.filter](/dataframe-docs#DataFrame.filter).





