nerdexam
Databricks

DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #95

Which of the following code blocks returns a new DataFrame with a new column customerSatisfactionAbs that is the absolute value of column customerSatisfaction in DataFrame storesDF? Note that column…

The correct answer is A. storesDF.withColumn("customerSatisfactionAbs", abs(col("customerSatisfaction"))). The correct syntax for withColumn is: DataFrame.withColumn(colName: String, col: Column). Option A correctly passes the new column name as a string ("customerSatisfactionAbs") and a Column expression (abs(col("customerSatisfaction"))). Option B uses withColumnRenamed, which…

Manipulate DataFrame Columns and Apply Transformations

Question

Which of the following code blocks returns a new DataFrame with a new column customerSatisfactionAbs that is the absolute value of column customerSatisfaction in DataFrame storesDF? Note that column customerSatisfactionAbs is not in the original DataFrame storesDF.

Options

  • AstoresDF.withColumn("customerSatisfactionAbs", abs(col("customerSatisfaction")))
  • BstoresDF.withColumnRenamed("customerSatisfactionAbs", abs(col("customerSatisfaction")))
  • CstoresDF.withColumn(col("customerSatisfactionAbs", abs(col("customerSatisfaction")))
  • DstoresDF.withColumn("customerSatisfactionAbs", abs(col(customerSatisfaction)))
  • EstoresDF.withColumn("customerSatisfactionAbs", abs("customerSatisfaction"))

How the community answered

(22 responses)
  • A
    91% (20)
  • B
    5% (1)
  • C
    5% (1)

Explanation

The correct syntax for withColumn is: DataFrame.withColumn(colName: String, col: Column). Option A correctly passes the new column name as a string ("customerSatisfactionAbs") and a Column expression (abs(col("customerSatisfaction"))). Option B uses withColumnRenamed, which renames an existing column - it does not add a new one. Option C has a syntax error (missing closing parenthesis for col()). Option D passes customerSatisfaction without quotes, treating it as a variable rather than a column name string. Option E passes a plain string to abs() instead of a Column object - abs() requires a Column, not a String.

Topics

#Spark DataFrames#Column Transformations#withColumn#Built-in Functions

Community Discussion

No community discussion yet for this question.

Full DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK Practice