
Answer-first summary for fast verification
Answer: assessPerformanceUDF = udf(assessPerformance, IntegerType()) storesDF.withColumn("result", assessPerformanceUDF(col("customerSatisfaction")))
The correct answer is B. To create a UDF with an integer return type, the return type must be specified as an instance of IntegerType(), using IntegerType(). Option B correctly initializes the UDF with the return type and applies it to the column. Other options have issues: A uses IntegerType (without parentheses), C has a syntax error and uses the wrong function, D omits the return type (defaulting to StringType), and E applies the Python function directly instead of the UDF.
Author: LeetQuiz Editorial Team
Ultimate access to all questions.
No comments yet.
Which of the following code blocks correctly creates a Python UDF assessPerformanceUDF() from the integer-returning Python function assessPerformance() and applies it to the column customerSatisfaction in DataFrame storesDF?
A
assessPerformanceUDF = udf(assessPerformance, IntegerType) storesDF.withColumn("result", assessPerformanceUDF(col("customerSatisfaction")))
B
assessPerformanceUDF = udf(assessPerformance, IntegerType()) storesDF.withColumn("result", assessPerformanceUDF(col("customerSatisfaction")))
C
assessPerformanceUDF - udf(assessPerformance) storesDF.withColumn("result", assessPerformance(col("customerSatisfaction")))
D
assessPerformanceUDF = udf(assessPerformance) storesDF.withColumn("result", assessPerformanceUDF(col("customerSatisfaction")))
E
assessPerformanceUDF = udf(assessPerformance, IntegerType()) storesDF.withColumn("result", assessPerformance(col("customerSatisfaction")))