QuestionQ221

VerifiedDeveloping Apache Spark™ DataFrame/DataSet API Applications

The code block below contains an error. It is intended to return a new DataFrame in which the managerName column from DataFrame storesDF is split at the space character into the managerFirstName and managerLastName columns. Identify the error.

A sample of DataFrame storesDF is shown below:

Question Image

Code block:

storesDF.withColumn("managerFirstName", col("managerName").split(" ").getItem(0))  
.withColumn("managerLastName", col("managerName").split(" ").getItem(1))  
Explanation

In PySpark, split is the pyspark.sql.functions.split function. It accepts a string column expression, including a Column object, and a delimiter pattern, then returns an array Column. Therefore it must be invoked as, for example, split(col("managerName"), " ").getItem(0). getItem(0) and getItem(1) correctly select the first and second split values.

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!