PySpark Aggregations
Assess your intermediate knowledge of PySpark Aggregations, focusing on real-world use cases and best practices.
Question 1 of 10
Question 1
How can you perform group-based aggregations in PySpark?
Not this time. In PySpark, group-based aggregations are performed using the groupBy method to specify the grouping column(s), followed by applying aggregation functions like sum, avg, or count
Question 2
Which PySpark function is used to compute aggregate statistics for numerical columns?
Not this time. describe() function in PySpark computes aggregate statistics like count, mean, standard deviation, minimum, and maximum for numerical columns in a DataFrame
Question 3
What is the result of df.groupBy("columnA").count()?
Not this time. df.groupBy("columnA").count() operation groups the DataFrame by the unique values in columnA and returns a DataFrame with each unique value and the number of rows corresponding to it
Question 4
How do you compute multiple aggregations for a single column in PySpark?
Not this time. Pass several aggregate expressions to agg(), for example df.groupBy("country").agg(F.sum("amount"), F.avg("amount"), F.max("amount")). A dictionary such as {"amount": "sum"} maps each column to only one function, so it can't compute two aggregations of the same column.
Question 5
What is the difference between count() and countDistinct()?
Not this time. count("*") counts rows and count("column") counts the column's non-null values, while countDistinct() counts the number of different non-null values.
Question 6
What is the difference between
groupByandrollupin PySpark?Not this time. groupBy() function groups data based on exact column values, while rollup() creates hierarchical subtotals for columns, making it useful for multi-level aggregations
Question 7
How can you pivot a DataFrame in PySpark for aggregations?
Not this time. pivot() function, used after groupBy(), reshapes the DataFrame by converting unique values of a specified column into new columns, enabling aggregations for each value
Question 8
Which function is used for hierarchical aggregations in PySpark?
Not this time. rollup() function is used for hierarchical aggregations, allowing you to compute subtotals and grand totals for multiple levels of grouping
Question 9
What are the benefits of using agg() instead of chaining multiple aggregation methods?
Not this time. agg() function allows you to perform multiple aggregations in a single call, making your code more concise, readable, and efficient compared to chaining multiple aggregation methods
Question 10
How do built-in aggregate functions such as sum() and avg() treat null values?
Not this time. Aggregate functions skip nulls: the average of 10, null and 20 is 15, and count("amount") counts only non-null values. If a null should count as a value (for example as 0), replace it first with fillna() or coalesce().
Sources
Where each fact comes from
Work it out
- CalculatorPartitions, brokers and disk for a Kafka topicMessages, their size, retention and readers in; the partitions, disk, network and brokers out.Kafka sizing calculator
- CalculatorSize a Spark or Databricks clusterThe Databricks workers to pick, or the executors, memory and shuffle partitions for your nodes, with every number explained.Spark sizing calculator