Databricks Certified Associate Developer For Apache Spark 30 · Free Practice Question Easy
Question 3
The code block shown below should return a DataFrame with column only aSquared dropped from DataFrame df. Choose the response that correctly fills in the numbered blanks within the code block to complete this task.
Code block:
df.__1__(__2__)
-
A
1. remove
2. “aSquared”
-
B
1. drop
2. “aSquared”
Reveal correct answer
Correct answer: B
Explanation
The drop() method of the DataFrame class in Apache Spark is used to drop a column from a DataFrame. It takes a single argument which is the name of the column to be dropped.
Here is an example of how to use drop() in Scala:
- val df = Seq((1, "Alice", 23), (2, "Bob", 35), (3, "Charlie", 45)).toDF("id", "name", "age")
- val newDf = df.drop("age")
This will produce a new DataFrame newDf with the same rows as the original DataFrame df, but with the column age removed:
- +---+-------+
- | id| name|
- +---+-------+
- | 1| Alice|
- | 2| Bob|
- | 3|Charlie|
- +---+-------+
In Python, you can use the following code to achieve the same result:
- from pyspark.sql import Row
- df = spark.createDataFrame([Row(id=1, name="Alice", age=23), Row(id=2, name="Bob", age=35), Row(id=3, name="Charlie", age=45)])
- newDf = df.drop("age")
Correct usage of drop function is the following:
- df.drop("col_name")
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
