nerdexam
Databricks

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

Which of the following code blocks returns a DataFrame with column storeSlogan where single quotes in column storeSlogan in DataFrame storesDF have been replaced with double quotes? A sample of…

The correct answer is C. storesDF.withColumn("storeSlogan", regexp_replace(col("storeSlogan"), "'", "\"")). regexp_replace(column, pattern, replacement) requires exactly three arguments: the column expression, the regex pattern to find, and the replacement string. Option C - storesDF.withColumn('storeSlogan', regexp_replace(col('storeSlogan'), "'", '"')) - is correct because it…

Working with DataFrames

Question

Which of the following code blocks returns a DataFrame with column storeSlogan where single quotes in column storeSlogan in DataFrame storesDF have been replaced with double quotes? A sample of DataFrame storesDF is below:

Exhibit

DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK question #169 exhibit

Options

  • AstoresDF.withColumn("storeSlogan", col("storeSlogan").regexp_replace("'" """))
  • BstoresDF.withColumn("storeSlogan", regexp_replace(col("storeSlogan"), "'"))
  • CstoresDF.withColumn("storeSlogan", regexp_replace(col("storeSlogan"), "'", """))
  • DstoresDF.withColumn("storeSlogan", regexp_replace("storeSlogan", "'", """))
  • EstoresDF.withColumn("storeSlogan", regexp_extract(col("storeSlogan"), "'", """))

How the community answered

(46 responses)
  • A
    4% (2)
  • B
    9% (4)
  • C
    72% (33)
  • D
    13% (6)
  • E
    2% (1)

Explanation

regexp_replace(column, pattern, replacement) requires exactly three arguments: the column expression, the regex pattern to find, and the replacement string. Option C - storesDF.withColumn('storeSlogan', regexp_replace(col('storeSlogan'), "'", '"')) - is correct because it passes a proper col() reference and both the search and replacement strings. Option A has broken syntax (missing comma between pattern and replacement). Option B is missing the third replacement argument entirely. Option D passes the column name as a plain string 'storeSlogan' rather than using col('storeSlogan') - regexp_replace requires a Column object as its first argument. Option E uses regexp_extract, which extracts a matched substring rather than replacing it.

Topics

#DataFrame Transformations#Spark SQL Functions#String Manipulation

Community Discussion

No community discussion yet for this question.

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