nerdexam
Databricks

DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #1

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?

The correct answer is E. storesDF.filter(col("sqft") <= 25000). In PySpark, column references must use the col() function from pyspark.sql.functions, passing the column name as a string. Option E - storesDF.filter(col("sqft") <= 25000) - is the only syntactically valid approach. Option A fails because Python string literals don't support…

Manipulating Data with Spark DataFrames

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?

Options

  • AstoresDF.filter("sqft" <= 25000)
  • BstoresDF.filter(sqft > 25000)
  • CstoresDF.where(storesDF[sqft] > 25000)
  • DstoresDF.where(sqft > 25000)
  • EstoresDF.filter(col("sqft") <= 25000)

How the community answered

(23 responses)
  • B
    4% (1)
  • E
    96% (22)

Explanation

In PySpark, column references must use the col() function from pyspark.sql.functions, passing the column name as a string. Option E - storesDF.filter(col("sqft") <= 25000) - is the only syntactically valid approach. Option A fails because Python string literals don't support comparison operators like <= in a way Spark can interpret. Options B and D fail because sqft is not a defined Python variable. Option C uses > instead of <= and also lacks quotes around sqft inside the bracket accessor. filter() and where() are aliases in Spark, so the method name is not the issue - the column reference syntax is.

Topics

#Spark DataFrame API#Filtering DataFrames#PySpark Syntax#Column Expressions

Community Discussion

No community discussion yet for this question.

Full DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK Practice