DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #112
Which of the following code blocks extracts the value for column sqft from the first row of DataFrame storesDF?
The correct answer is E. storesDF.first().sqft. In PySpark, DataFrame.first() returns the first Row object. A Row in PySpark supports attribute-style access using dot notation, so storesDF.first().sqft correctly retrieves the value of the sqft column from the first row. Choice A fails because col('sqft') returns a Column…
Question
Which of the following code blocks extracts the value for column sqft from the first row of DataFrame storesDF?
Options
- AstoresDF.first()[col("sqft")]
- BstoresDF[0]["sqft"]
- CstoresDF.collect(l)[0]["sqft"]
- DstoresDF.first.sqft
- EstoresDF.first().sqft
How the community answered
(19 responses)- B5% (1)
- D11% (2)
- E84% (16)
Explanation
In PySpark, DataFrame.first() returns the first Row object. A Row in PySpark supports attribute-style access using dot notation, so storesDF.first().sqft correctly retrieves the value of the sqft column from the first row. Choice A fails because col('sqft') returns a Column object, which cannot be used to index a Row. Choice B fails because DataFrames do not support integer-based row indexing like lists. Choice C has a syntax error - collect() takes no arguments. Choice D fails because first without parentheses is a method reference, not a method call, so .sqft would fail.
Topics
Community Discussion
No community discussion yet for this question.