
Ultimate access to all questions.
Answer-first summary for fast verification
Answer: df.withColumn('first_word', split(col('description'), ' ')[0])
The correct answer is A because it correctly uses the `split` function to divide the `description` column by spaces and selects the first element, effectively extracting the first word.
Author: LeetQuiz Editorial Team
No comments yet.
Given a DataFrame df with a column description containing strings, how would you extract the first word from this column using Spark? Provide the code snippet.
A
df.withColumn('first_word', split(col('description'), ' ')[0])
B
df.select(split(col('description'), ' ')[0])
C
df.withColumn('first_word', substring(col('description'), 0, indexOf(col('description'), ' ')))
D
df.select(substring(col('description'), 0, indexOf(col('description'), ' ')))