DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #84
The code block shown below contains an error. The code block is intended to return a new DataFrame that is the result of a position-wise union between DataFrame storesDF and DataFrame…
The correct answer is E. storesDF.union(acquiredStoresDF). storesDF.union(acquiredStoresDF) is the correct PySpark syntax because union() is an instance method on a DataFrame object that performs a position-wise row combination - equivalent to SQL's UNION ALL. Why the distractors fail: A & C - concat() and union() as standalone…
Question
The code block shown below contains an error. The code block is intended to return a new DataFrame that is the result of a position-wise union between DataFrame storesDF and DataFrame acquiredStoresDF.
Options
- Aconcat(storesDF, acquiredStoresDF)
- BstoresDF.unionByName(acquiredStoresDF)
- Cunion(storesDF, acquiredStoresDF)
- DunionAll(storesDF, acquiredStoresDF)
- EstoresDF.union(acquiredStoresDF)
How the community answered
(45 responses)- B2% (1)
- C2% (1)
- D4% (2)
- E91% (41)
Explanation
storesDF.union(acquiredStoresDF) is the correct PySpark syntax because union() is an instance method on a DataFrame object that performs a position-wise row combination - equivalent to SQL's UNION ALL.
Why the distractors fail:
- A & C -
concat()andunion()as standalone functions don't exist in PySpark's DataFrame API;concatis a string/array column function, not a DataFrame-level one. - B -
unionByName()is a real PySpark method, but it matches columns by name, not by position - the wrong behavior for this task. - D -
unionAll()was the original method but was deprecated in Spark 2.0 and replaced byunion(); calling it as a standalone function is doubly wrong.
Memory tip: In PySpark, DataFrame operations are almost always method calls on an existing DataFrame (df.someOperation()), not standalone functions. If you see a union operation written without a DataFrame receiver (like options A, C, D), it's a red flag.
Topics
Community Discussion
No community discussion yet for this question.