Databricks Certified Associate Developer For Apache Spark 30 · Free Practice Question Easy
Question 7
Which of the following code blocks concatenates two DataFrames df1 and df2 ?
-
A
df1.addAll(df2) -
B
df1.append(df2) -
C
df1.add(df2) -
D
df1.appendAll(df2) -
E
df1.union(df2)
Reveal correct answer
Correct answer: E
Explanation
DataFrames are immutable. This means users cannot append to DataFrames because that would be changing it. To append to a DataFrame, you must union the original DataFrame along with the new DataFrame.
To concatenate two DataFrames in Apache Spark, you can use the union() method of the DataFrame class. This method combines the rows of two DataFrames into a single DataFrame.
Here is an example of how to use union() in Scala:
- val df1 = Seq((1, "Alice", 23), (2, "Bob", 35)).toDF("id", "name", "age")
- val df2 = Seq((3, "Charlie", 45), (4, "Dave", 28)).toDF("id", "name", "age")
- val df3 = df1.union(df2)
This will produce a new DataFrame df3 that includes all the rows from both df1 and df2:
- +---+-------+---+
- | id| name|age|
- +---+-------+---+
- | 1| Alice| 23|
- | 2| Bob| 35|
- | 3|Charlie| 45|
- | 4| Dave| 28|
- +---+-------+---+
In Python, you can use the following code to achieve the same result:
- from pyspark.sql import Row
- df1 = spark.createDataFrame([Row(id=1, name="Alice", age=23), Row(id=2, name="Bob", age=35)])
- df2 = spark.createDataFrame([Row(id=3, name="Charlie", age=45), Row(id=4, name="Dave", age=28)])
- df3 = df1.union(df2)
Therefore, the code block that concatenates two DataFrames df1 and df2 is:
- df3 = df1.union(df2)
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
