nerdexam
Databricks

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

Which of the following code blocks applies the function assessPerformance() to each row of DataFrame storesDF?

The correct answer is D. [assessPerformance(row) for row in storesDF.collect()]. [assessPerformance(row) for row in storesDF.collect()] is correct: storesDF.collect() returns a Python list of all Row objects, and the list comprehension then calls assessPerformance(row) on each one. Option A only iterates over storesDF.take(3), which limits to the first 3…

Working with Spark DataFrames

Question

Which of the following code blocks applies the function assessPerformance() to each row of DataFrame storesDF?

Options

  • A[assessPerformance(row) for row in storesDF.take(3)]
  • B[assessPerformance() for row in storesDF]
  • CstoresDF.collect().apply(lambda: assessPerformance)
  • D[assessPerformance(row) for row in storesDF.collect()]
  • E[assessPerformance(row) for row in storesDF]

How the community answered

(28 responses)
  • A
    11% (3)
  • B
    4% (1)
  • C
    4% (1)
  • D
    82% (23)

Explanation

[assessPerformance(row) for row in storesDF.collect()] is correct: storesDF.collect() returns a Python list of all Row objects, and the list comprehension then calls assessPerformance(row) on each one. Option A only iterates over storesDF.take(3), which limits to the first 3 rows. Option B iterates over a raw DataFrame (not iterable in PySpark) and does not pass row to the function. Option C calls .apply() on a Python list, which has no such method. Option E iterates directly over the DataFrame object, which is not supported in PySpark - you must first call .collect() to get an iterable Python list.

Topics

#Spark DataFrames#DataFrame Actions#Python List Comprehensions#Row-wise Operations

Community Discussion

No community discussion yet for this question.

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