nerdexam
Databricks

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

Which of the following code blocks returns a DataFrame sorted alphabetically based on column division?

The correct answer is D. storesDF.orderBy("division", ascending - true). Alphabetical sorting means ascending order. Option D - storesDF.orderBy('division', ascending=True) (note: the dash in ascending - true in the question text is a formatting artifact for =) - explicitly sorts ascending by 'division'. Options B and C use desc(), which sorts…

Transforming Data with Spark DataFrames

Question

Which of the following code blocks returns a DataFrame sorted alphabetically based on column division?

Options

  • AstoresDF.sort("division")
  • BstoresDF.orderBy(desc("division"))
  • CstoresDF.orderBy(col("division").desc())
  • DstoresDF.orderBy("division", ascending - true)
  • EstoresDF.sort(desc("division"))

How the community answered

(14 responses)
  • B
    7% (1)
  • D
    93% (13)

Explanation

Alphabetical sorting means ascending order. Option D - storesDF.orderBy('division', ascending=True) (note: the dash in ascending - true in the question text is a formatting artifact for =) - explicitly sorts ascending by 'division'. Options B and C use desc(), which sorts descending (reverse alphabetical). Options E also uses desc(). Option A - storesDF.sort('division') - does sort ascending by default and would technically work, but option D is preferred as it explicitly declares the sort direction with ascending=True, making the intent unambiguous and matching the canonical orderBy method.

Topics

#DataFrame Operations#Sorting DataFrames#orderBy Function#Spark SQL API

Community Discussion

No community discussion yet for this question.

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