STRUCTURED APISPySpark

Core DataFrame Operations

Distributed data processing with Spark — pick a topic on the left and its full breakdown loads here: the execution model, worked jobs and diagrams, performance and shuffle behavior, and the habits that keep Spark jobs fast, correct, and affordable.

18 min readTopics chapter readerLevel · Structured APIs
01 · Orientation

What You'll Master Here

pick columns (select/selectExpr), keep rows (filter), compute new columns (withColumn/lit/cast/when), then maybe order/trim. Do the cheap, shrinking steps first, and treat null as a value that needs a deliberate decision at every step.

4 min · Topic 1 of 8

This is the bread and butter of Spark: the handful of DataFrame operations you will use in literally every job. select, selectExpr, and filter to choose columns and rows; withColumn, lit, cast, when/otherwise, withColumnRenamed, and drop to shape columns; and sort, distinct, and limit to order and trim.

None of this is hard, but doing it cleanly, knowing which operations are cheap (narrow) versus expensive (wide), and knowing where nulls quietly change your answer, is what separates readable, fast, correct code from slow, tangled, silently-wrong code. Recall from Chapter 5 that wide transformations trigger a shuffle while narrow ones do not — that distinction is exactly what tells you which of these everyday operations to reach for freely and which to use sparingly. We will also cover chaining operations into a clear pipeline.

Every operation comes with input and output so you can see exactly what it does, including the traps: a null that disappears from a filter, a cast that returns null instead of erroring, a dropDuplicates that keeps a different row each run.

Core mental model

Most jobs are: pick columns (select/selectExpr), keep rows (filter), compute new columns (withColumn/lit/cast/when), then maybe order/trim. Do the cheap, shrinking steps first, and treat null as a value that needs a deliberate decision at every step.

Why it matters

These operations make up the large majority of day-to-day Spark transformations. Writing them clearly, ordering them well (filter early, sort late), and knowing their edge cases is what makes your jobs readable, efficient, and trustworthy.

select / selectExpr
Choose / compute the columns to keep, with typed Columns or SQL-expression strings.
filter / where
Keep only the rows matching a condition (identical methods).
withColumn
Add or replace one column from an expression.
orderBy / sort
Sort rows; a wide transformation (it shuffles).
Common mistake

Filtering and selecting late, after heavy steps. You carry more data than needed into joins/shuffles; filter and select early to shrink data first.

Better habit

Filter and select as early as possible to reduce data.

Use withColumn for derived fields; select to project.

Remember orderBy and distinct are wide (shuffles), use them only when needed.

The big idea

A few composable operations cover most transformations. The skill is ordering them well, shrink early with filter/select, avoid needless wide ops like sort and distinct, and handle nulls and casts deliberately rather than by accident.

Remember this

select/selectExpr/filter/withColumn/cast/when/rename/drop/sort/distinct/limit cover most transformations. Apply the cheap, data-shrinking ones early; reserve wide ones (sort, distinct) for when you truly need them; and watch nulls at every step.

Practice2 prompts
  1. Which of these are narrow and which are wide: select, filter, orderBy, withColumn, distinct?
  2. Why should filter usually come before a join?