Filtering PySpark DataFrames with multiple conditions
Learn how to combine Column conditions in PySpark filter and where calls, why parentheses matter, and how to write the same rules as a Spark SQL string.
Contents
- Combining conditions with Column operators
- Why parentheses decide the result
- Writing the same rules in Spark SQL
- PySpark multiple condition filter questions
- Is there a difference between filter and where in PySpark?
- How do I filter rows where a column is in a list of values?
- How do null values affect a filter with multiple conditions?
- Can I build multiple filter conditions dynamically from a list?
Filtering PySpark DataFrames with multiple conditions means joining Column conditions with operators &, | and ~ inside DataFrame.filter. Each condition must sit in its own parentheses, because Python evaluates & and | before comparisons. The method where is an alias of filter, so both calls return the same rows. You can also pass a Spark SQL string when the rules read more clearly as a where clause.
Combining conditions with Column operators
Python Spark code that filters on multiple conditions uses & for and, | for or, and ~ for not. The Python keywords and, or and not fail on Column objects and raise a ValueError. A shortlist rule might read df.filter((col('stock') > 0) & (col('status') == 'active')). Filters can also be chained, since df.filter(a).filter(b) keeps only rows that pass both.
Why parentheses decide the result
Parentheses around each comparison in a PySpark filter are required by Python operator precedence. Python binds & and | more tightly than comparison operators such as == or >. Writing col('stock') > 0 & col('rating') > 3 makes Python evaluate 0 & col('rating') first. The leftover chained comparison then raises an error instead of applying the rule you meant.
Writing the same rules in Spark SQL
Spark SQL syntax offers a second way to filter a DataFrame on several rules at once. A call such as df.where('stock > 0 AND region IS NOT NULL') accepts a plain SQL where clause. Inside these strings, the keywords AND, OR and NOT replace the Python symbols &, | and ~. Both styles compile to the same query plan, so the choice comes down to readability.
PySpark multiple condition filter questions
Is there a difference between filter and where in PySpark?
No, where is an alias of DataFrame.filter in PySpark and behaves exactly the same. Both method names accept either a Column expression or a Spark SQL string. The where name exists as a familiar option for readers coming from SQL.
How do I filter rows where a column is in a list of values?
The isin method on a Column filters a PySpark DataFrame against a list of values. A call like df.filter(col('status').isin(['active', 'pending'])) keeps only matching rows. Placing ~ in front of the isin condition keeps the rows outside the list instead. Because isin returns a Column, it combines with & and | like any other condition.
How do null values affect a filter with multiple conditions?
Null values make a PySpark comparison return null, and filter drops every row with a null result. A row with a missing rating fails col('rating') > 3 and also fails its negation with ~. Adding isNull or isNotNull to the combined condition lets you keep or exclude those rows on purpose. Under |, null or true evaluates to true, so such a row can still pass.
Can I build multiple filter conditions dynamically from a list?
Yes, a list of PySpark Column conditions can be joined into one filter with functools.reduce. The expression reduce(lambda a, b: a & b, conditions) combines every rule with a logical and. Swapping & for | gives a filter that keeps rows matching any single rule. Such a pattern suits shortlists where readers add or remove their own criteria.