Vega-Lite Filter Transform
Use filter inside transform to keep only rows that match a condition.
{
"transform": [
{"filter": ...}
]
}This page covers the most common vega lite filter and altair transform filter patterns.
Filter Syntax (Quick Reference)
| Use case | Vega-Lite filter |
|---|---|
| Expression | {"filter": "datum.Miles_per_Gallon > 30"} |
| Category include list | {"filter": {"field": "Origin", "oneOf": ["USA", "Japan"]}} |
| Numeric range | {"filter": {"field": "Horsepower", "range": [80, 150]}} |
| Remove null/NaN values | {"filter": {"field": "Miles_per_Gallon", "valid": true}} |
| Multiple conditions | {"filter": {"and": [{"field": "Miles_per_Gallon", "gte": 30}, {"field": "Weight_in_lbs", "lt": 3000}]}} |
You can also use or and not inside logical predicates.
Example 1: Basic Vega Filter Expression
Keep only cars with MPG above 30.
{
"data": {"url": "data/cars.json"},
"transform": [
{"filter": "datum.Miles_per_Gallon > 30"}
],
"mark": "point",
"encoding": {
"x": {"field": "Horsepower", "type": "quantitative"},
"y": {"field": "Miles_per_Gallon", "type": "quantitative"}
}
}No Filter
Filtered
Example 2: Transform Filter with Predicate Object
This version avoids raw expression strings and is easier to maintain.
{
"data": {"url": "data/cars.json"},
"transform": [
{
"filter": {
"and": [
{"field": "Miles_per_Gallon", "gte": 30},
{"field": "Weight_in_lbs", "lt": 3000}
]
}
}
],
"mark": "point",
"encoding": {
"x": {"field": "Horsepower", "type": "quantitative"},
"y": {"field": "Miles_per_Gallon", "type": "quantitative"},
"color": {"field": "Origin", "type": "nominal"}
}
}Example 3: Category Filter (oneOf)
Use oneOf when you need an explicit allow-list.
{
"data": {"url": "data/cars.json"},
"transform": [
{"filter": {"field": "Origin", "oneOf": ["USA", "Japan"]}}
],
"mark": "bar",
"encoding": {
"x": {"field": "Origin", "type": "nominal"},
"y": {"aggregate": "count", "type": "quantitative"}
}
}Altair transform_filter Patterns
# 1) Expression string
chart.transform_filter("datum.Origin == 'USA'")
# 2) Datum expression
chart.transform_filter(
(alt.datum.Horsepower > 120) &
(alt.datum.Miles_per_Gallon > 20)
)
# 3) Predicate object
chart.transform_filter(
alt.FieldOneOfPredicate(field="Origin", oneOf=["USA", "Japan"])
)Common Mistakes
- Filtering with a missing field (for example
Priceincars.json) returns empty or unexpected results. - Not handling null values for numeric fields. Use
{"valid": true}when needed. - Mixing string and number comparisons (
"30"vs30) in expressions.
FAQ
What is the difference between Vega filter and Vega-Lite filter?
The idea is the same: remove rows before drawing marks. Vega-Lite provides a concise declarative syntax in the transform array.
How do I write transform filter for multiple conditions?
Use either an expression string (&&, ||) or a logical predicate object with and / or / not.
Why does my chart show no points after filtering?
Check field names, data types, and null values first. In this page's examples, use Weight_in_lbs instead of non-existent fields like Price.