Databricks Certified Associate Developer For Apache Spark 30 · Free Practice Question Medium
Question 15
- A DataFrame.drop()
- B DataFrame.withColumn()
- C DataFrame.withColumnRenamed()
- D DdataFrame.head()
- E DataFrame.filter()
Reveal correct answer
Correct answer: B
Explanation
withColumn() method of the DataFrame class is used to add a new column to a DataFrame or to replace the value of an existing column. It takes two arguments: the name of the new column and the value to be placed in the column. The value can be a literal value, a column reference, or a function that generates a value for each row.
For example, consider the following DataFrame df:
- +---+------+
- | id| name |
- +---+------+
- | 1|Alice |
- | 2| Bob |
- | 3|Charlie|
- +---+------+
To add a new column age with a constant value of 25 for all rows, you can use the following code:
- import org.apache.spark.sql.functions._
- val df2 = df.withColumn("age", lit(25))
This will produce the following DataFrame:
- +---+------+---+
- | id| name |age|
- +---+------+---+
- | 1|Alice | 25|
- | 2| Bob | 25|
- | 3|Charlie| 25|
- +---+------+---+
To add a new column price that is calculated based on the value of the id column, you can use the following code:
- val df2 = df.withColumn("price", col("id") * 10)
This will produce the following DataFrame:
- +---+------+-----+
- | id| name |price|
- +---+------+-----+
- | 1|Alice | 10 |
- | 2| Bob | 20 |
- | 3|Charlie| 30 |
- +---+------+-----+
To replace the value of an existing column, you can use the same syntax as above and specify the name of the existing column as the first argument. For example, to replace the values of the name column with the lowercase version of the values, you can use the following code:
- val df2 = df.withColumn("name", lower(col("name")))
- ``
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
