The code block shown below contains an error. The code block is intended to return a new DataFrame with the mean of column sqft from DataFrame storesDF in column sqftMean. Identify the error. Code block: storesDF.agg(mean("sqft").alias("sqftMean"))
Select an option, then click Submit answer.
Reference / correct answer:
The argument to the mean() operation should be a Column abject rather than a string column name.
Most accepted answer: A. The argument to the mean() operation should be a Column abject rather than a string column name.
Community votes: A=5, D=1, E=1
Selected Answer: E The code block shown is correct and should return a new DataFrame with the mean of column sqft from DataFrame storesDF in column sqftMean. Therefore, the answer is E - none of the options identify a valid error in the code block. Here's an explanation for each option: A. The argument to the mean() operation can be either a Column object or a string column name, so there is no error in using a string column name in this case. E. This option is incorrect because the code block shown is a valid way to compute the mean of a column using PySpark. Another way to compute the mean of a column is with the mean() method from a DataFrame, but that doesn't mean the code block shown is invalid. upvoted 7 times newusername 2 years, 8 months ago wrong! A upvoted 3 times ...
wrong! A upvoted 3 times
Selected Answer: A The function mean() is part of pyspark.sql.functions, and it expects a Column object, not a string. upvoted 1 times
The mean() function expects a Column object as an argument, which can be created using col("sqft"). Simply passing the column name as a string will result in an error. upvoted 2 times
The correct answer is A. The argument to the mean() operation should be a Column object rather than a string column name. In Spark DataFrames, the mean() function takes a Column object as its argument, not a string column name. To create a Column object from a string column name, you can use the col() function. upvoted 1 times