Skip to main content
The groupBy() command in Spark is used to group rows in a DataFrame based on one or more columns. It is typically followed by an aggregation function (e.g., count(), sum(), avg(), etc.) to perform calculations on the grouped data. This is particularly useful for summarizing and analyzing data.

1. Syntax

PySpark:
Spark SQL:

2. Parameters

  • cols: A list of column names (as strings) or column objects to group the data by.

3. Return Type

  • Returns a GroupedData object, which can be used to apply aggregation functions.

4. Common Aggregation Functions

  • count(): Count the number of rows in each group.
  • sum(): Calculate the sum of a numeric column for each group.
  • avg(): Calculate the average of a numeric column for each group.
  • min(): Find the minimum value in a column for each group.
  • max(): Find the maximum value in a column for each group.

5. Examples

Example 1: Grouping by a Single Column and Counting Rows

PySpark:
Spark SQL:
Output:

Example 2: Grouping by Multiple Columns and Calculating Aggregations

PySpark:
Spark SQL:
Output:

Example 3: Grouping and Finding Minimum and Maximum Values

PySpark:
Spark SQL:
Output:

Example 4: Grouping and Using Multiple Aggregations

PySpark:
Spark SQL:
Output:

Example 5: Grouping and Aggregating with Null Values

PySpark:
Spark SQL:
Output:

Example 6: Grouping and Aggregating with Custom Logic

PySpark:
Spark SQL:
Output:

6. Common Use Cases

  • Calculating summary statistics (e.g., total sales by region).
  • Analyzing trends or patterns in data (e.g., average salary by department).
  • Preparing data for machine learning by creating aggregated features.

7. Performance Considerations

  • Use groupBy() judiciously on large datasets, as it involves shuffling and sorting, which can be expensive.
  • Consider using repartition() or coalesce() to optimize performance when working with large datasets.

8. Key Takeaways

  1. The groupBy() command is used to group rows in a DataFrame based on one or more columns.
  2. It can be combined with various aggregation functions to summarize data.
  3. Grouping and aggregating data can be resource-intensive for large datasets, as it involves shuffling and sorting.
  4. In Spark SQL, similar functionality can be achieved using GROUP BY with aggregation functions.
  5. Works efficiently on large datasets when combined with proper partitioning and caching.