DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #2
Which of the following code blocks returns a DataFrame containing only the rows from DataFrame storesDF where the value in column sqft is less than or equal to 25,000 OR the value in column…
The correct answer is E. storesDF.filter((col("sqft") <= 25000) | (col("customerSatisfaction") >= 30)). When combining Spark column conditions with OR, you must use the bitwise | operator (not Python's or keyword) and wrap each individual condition in parentheses. This is required because of Python operator precedence: without parentheses, | binds more tightly than comparison…
Question
Which of the following code blocks returns a DataFrame containing only the rows from DataFrame storesDF where the value in column sqft is less than or equal to 25,000 OR the value in column customerSatisfaction is greater than or equal to 30?
Options
- AstoresDF.filter(col("sqft") <= 25000 | col("customerSatisfaction") >= 30)
- BstoresDF.filter(col("sqft") <= 25000 or col("customerSatisfaction") >= 30)
- CstoresDF.filter(sqft <= 25000 or customerSatisfaction >= 30)
- DstoresDF.filter(col(sqft) <= 25000 | col(customerSatisfaction) >= 30)
- EstoresDF.filter((col("sqft") <= 25000) | (col("customerSatisfaction") >= 30))
How the community answered
(28 responses)- A7% (2)
- B4% (1)
- C18% (5)
- E71% (20)
Explanation
When combining Spark column conditions with OR, you must use the bitwise | operator (not Python's or keyword) and wrap each individual condition in parentheses. This is required because of Python operator precedence: without parentheses, | binds more tightly than comparison operators like <= and >=, causing incorrect evaluation. Option A is missing the parentheses around each condition, making it evaluate as col("sqft") <= (25000 | col(...)) >= 30, which is wrong. Options B and C use Python's or keyword, which doesn't work with Spark Column objects. Option D passes unquoted variable names to col(). Only E - (col("sqft") <= 25000) | (col("customerSatisfaction") >= 30) - is correct.
Topics
Community Discussion
No community discussion yet for this question.