nerdexam
Databricks

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

The code block shown below should return a new DataFrame where single quotes in column storeSlogan have been replaced with double quotes. Choose the response that correctly fills in the numbered…

The correct answer is D. 1. withColumn. To replace single quotes with double quotes in a string column, you use regexp_replace() from pyspark.sql.functions. The correct pattern is: storesDF.withColumn('storeSlogan', regexp_replace(col('storeSlogan'), "'", '"')). So blank 1 = withColumn, blank 2 = 'storeSlogan' (the…

DataFrame Transformations

Question

The code block shown below should return a new DataFrame where single quotes in column storeSlogan have been replaced with double quotes. Choose the response that correctly fills in the numbered blanks within the code block to complete this task. A sample of DataFrame storesDF is below:

Code block:

storesDF.1(2, 3(4, 5, 6))

Options

  • A
    1. withColumn
  • B
    1. newColumn
  • C
    1. withColumn
  • D
    1. withColumn
  • E
    1. withColumn

How the community answered

(53 responses)
  • A
    15% (8)
  • B
    8% (4)
  • C
    4% (2)
  • D
    70% (37)
  • E
    4% (2)

Explanation

To replace single quotes with double quotes in a string column, you use regexp_replace() from pyspark.sql.functions. The correct pattern is: storesDF.withColumn('storeSlogan', regexp_replace(col('storeSlogan'), "'", '"')). So blank 1 = withColumn, blank 2 = 'storeSlogan' (the column to overwrite), blank 3 = regexp_replace, blank 4 = col('storeSlogan'), blank 5 = "'" (the pattern to find - a single quote), and blank 6 = '"' (the replacement - a double quote). Option D is the only choice that correctly combines withColumn with regexp_replace and the proper argument structure.

Topics

#PySpark DataFrame API#Column Transformations#String Functions#Data Manipulation

Community Discussion

No community discussion yet for this question.

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