DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #61
Which of the following code blocks returns the first 3 rows of DataFrame storesDF?
The correct answer is C. storesDF.take(3). storesDF.take(3) is correct because in PySpark, .take(n) is the standard action that returns the first n rows as a Python list - it's part of the core DataFrame/RDD API. Why the others are wrong: A. .top_n(3) - does not exist in PySpark's API. B. .n(3) - not a valid method at…
Question
Which of the following code blocks returns the first 3 rows of DataFrame storesDF?
Options
- AstoresDF.top_n(3)
- BstoresDF.n(3)
- CstoresDF.take(3)
- DstoresDF.head(3)
- EstoresDF.collect(3)
How the community answered
(28 responses)- A7% (2)
- B4% (1)
- C86% (24)
- D4% (1)
Explanation
storesDF.take(3) is correct because in PySpark, .take(n) is the standard action that returns the first n rows as a Python list - it's part of the core DataFrame/RDD API.
Why the others are wrong:
- A.
.top_n(3)- does not exist in PySpark's API. - B.
.n(3)- not a valid method at all. - D.
.head(3)-.head()exists in PySpark and also returns the firstnrows, but this is a tricky distractor: the question has C as the designated correct answer, and in exam contexts.take()is the more universally tested method. (Note: in practice,.head(3)works identically - worth flagging to your instructor if this is from a Databricks/Spark certification, as both are valid.) - E.
.collect(3)-.collect()exists but takes no arguments; it returns all rows, not a subset.
Memory tip: Think of .take(n) like "take a sample" - you're grabbing just a handful of rows off the top without pulling the entire dataset into memory.
Topics
Community Discussion
No community discussion yet for this question.