Skip to main content

Module 6: Spark DataFrames and Pipelines

Overview

Most day-to-day Spark work uses the structured APIs: DataFrames and Spark SQL. This module explains why they replaced RDDs for data science, then works through data wrangling on a household survey dataset: creating DataFrames, defining schemas, working with columns and expressions, filtering, grouping and aggregating, user-defined functions, and moving between Spark, SQL and pandas.

Estimated time: about 4 hours (1 hour lecture, 3 hours hands-on)

Learning objectives

By the end of this module, you should be able to:

  • Compare RDDs with the structured APIs, and choose between RDDs, DataFrames and Spark SQL for a given task.
  • Create Spark DataFrames from files and Python objects, and define schemas with Spark’s data types.
  • Select, add, rename and filter columns using expressions and Spark’s built-in functions.
  • Aggregate data with groupBy, and query the same data with Spark SQL.
  • Write user-defined functions, and convert between Spark DataFrames, pandas DataFrames and RDDs.

Before class

Make sure PySpark works on your machine: follow Python environment setup and run the check-setup notebook.

Prerequisites: Module 5, especially transformations, actions and lazy evaluation. Experience with pandas or SQL helps.

The exercises use the household dataset; see data/README.md for how to get it.

Lecture slides

Open the slides (Google Slides) Version française

Hands-on

ImportantCommon error: Java version

Spark notebooks fail with UnsupportedClassVersionError or Java gateway process exited when Java is missing or too old. PySpark 4 needs Java 17 or later. See Python environment setup → Troubleshooting.

Description of the Household Dataset

The census-style household dataset used in this module’s exercises.

View Open in Colab Download

Spark Structured APIs

Create DataFrames, define schemas, and compare loading with and without a schema.

View Open in Colab Download

Working with Spark DataFrames

Schemas, manipulating columns, filtering, grouping, user-defined functions, and converting to pandas and RDDs.

View Open in Colab Download

Exercises

To be added.

Further reading