DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK-3-0 practice question 4 of 25

Which of the following code blocks returns a 2-column DataFrame that shows the distinct values in column productId and the number of rows with that productId in DataFrame transactionsDf?

Full set: 180 questions
Answer options
A: transactionsDf.count("productId").distinct()
B: transactionsDf.groupBy("productId").agg(col("value").count())
C: transactionsDf.count("productId")
D: transactionsDf.groupBy("productId").count() Correct answer
E: transactionsDf.groupBy("productId").select(count("value"))
Correct Answer:
D. transactionsDf.groupBy("productId").count()
Explanation

transactionsDf.groupBy("productId").count() Correct. This code block first groups DataFrame transactionsDf by column productId and then counts the rows in each group. transactionsDf.groupBy("productId").select(count("value")) Incorrect. You cannot call select on a GroupedData object (the output of a groupBy) statement. transactionsDf.count("productId") No. DataFrame.count() does not take any arguments. transactionsDf.count("productId").distinct() Wrong. Since DataFrame.count() does not take any arguments, this option cannot be right. transactionsDf.groupBy("productId").agg(col("value").count()) False. A Column object, as returned by col("value"), does not have a count() method. You can see all available methods for Column object linked in the Spark documentation below. More info: pyspark.sql.DataFrame.count — PySpark 3.1.2 documentation, pyspark.sql.Column — PySpark 3.1.2 documentation Static notebook | Dynamic notebook: See test 3,

Scroll to Top