Menu

PySpark course · Lesson 1 of 12

PySpark Fundamentals for Data Engineers

PySpark fundamentals in one guide: DataFrames and schemas, lazy evaluation, joins, window functions, UDF alternatives, partitions, shuffles and debugging.

  • Beginner
  • Pillar guide
  • 9 min read
  • Updated Oct 2026
On this page
  1. Where PySpark fits in a data platform
  2. Core concepts
  3. A worked example you can run
  4. PySpark vs pandas (and other alternatives)
  5. What this course covers
  6. Checkpoints
  7. Common beginner mistakes
  8. What interviewers ask
  9. Key takeaway

PySpark is the Python API for Apache Spark, a distributed engine that processes data in parallel across many machines. You write code that looks like SQL or pandas (select, filter, groupBy, join), and Spark turns it into an optimised plan that runs on a cluster of executors. Because the code runs lazily and in parallel, correctness and performance depend on a few ideas that pandas never forces you to think about: schemas, lazy evaluation, partitions and shuffles.

This page is the course map: where PySpark fits, the core concepts with a runnable example, how it compares with pandas and SQL engines, the lessons in order, the mistakes beginners make and what interviewers ask.

Where PySpark fits in a data platform

In most data platforms PySpark is the batch and streaming transformation engine between raw storage and curated tables:

  • Input: files in object storage (Parquet, JSON, CSV, Avro), lakehouse tables (Delta Lake, Iceberg), databases over JDBC, Kafka topics.
  • Processing: cleaning, deduplication, joins, aggregations, sessionisation, feature engineering, usually written as DataFrame code or Spark SQL.
  • Output: partitioned Parquet or Delta tables that a warehouse, BI tool or ML job reads.
  • Runs on: Databricks, Amazon EMR, AWS Glue, Google Dataproc, Kubernetes, or a laptop in local mode. An orchestrator such as Airflow or Lakeflow Jobs schedules it.

Use it when data is too large for one machine, when the same code must scale from gigabytes to terabytes, or when you need Spark’s streaming, connectors and lakehouse integration. For data that fits comfortably in memory, a single-node tool is simpler and faster.

Core concepts

  • SparkSession is the entry point: spark.read, spark.createDataFrame, spark.sql. On Databricks it already exists as spark.
  • DataFrame: an immutable, distributed table with a schema (column names and types). Every transformation returns a new DataFrame. Define schemas explicitly for production reads; inference is slow on large inputs and guesses wrong.
  • Transformations vs actions. select, filter, withColumn, join and groupBy().agg() are transformations: they only add steps to a logical plan. Actions such as count, collect, show and write run the plan. Spark’s Catalyst optimiser rewrites the whole plan first (pushing filters down, pruning columns, choosing join strategies).
  • Partitions are the chunks of data that tasks process in parallel. Narrow transformations (filter, select) work within a partition; wide ones (groupBy, join, orderBy, distinct) need a shuffle that moves rows across the network and starts a new stage. Shuffles and skew (a few keys holding most rows) cause most performance problems.
  • Driver and executors. Your Python program runs on the driver, which plans the job; executors run the tasks. collect() pulls every row back to the driver, which is how small laptops and large clusters both run out of memory.
  • ANSI mode is on by default in Spark 4. Invalid casts and arithmetic overflow raise errors instead of silently producing NULL. Use try_cast (or the try_* functions) when bad values are expected and should become NULL.
  • Built-in functions before UDFs. Functions in pyspark.sql.functions run inside the JVM engine. Python UDFs move every row to a Python worker and hide logic from the optimiser; when you need custom Python, prefer vectorised pandas UDFs.
  • Spark Connect. Spark 4 clients can talk to a remote Spark server through Spark Connect (serverless Databricks uses it). The DataFrame and SQL APIs are the same; low-level sparkContext and RDD access are not available there.

A worked example you can run

This example reads a small CSV with an explicit schema, handles a bad value safely, joins to a small dimension, computes revenue per category, ranks products within each category with a window function, and prints the physical plan. Install PySpark 4.x with pip install pyspark (Java 17 or 21 is required) and run it as a script.

import os, tempfile
from pyspark.sql import SparkSession, Window, functions as F
from pyspark.sql.types import StructType, StructField, StringType, IntegerType

spark = (SparkSession.builder.master("local[2]").appName("pyspark-fundamentals")
         .config("spark.sql.shuffle.partitions", "4")      # small data: avoid 200 tiny tasks
         .getOrCreate())

# 1. Raw input: a CSV file with one malformed price
path = os.path.join(tempfile.mkdtemp(), "orders.csv")
with open(path, "w") as f:
    f.write("order_id,product_id,qty,price\n"
            "1,p1,2,10.00\n2,p2,1,25.50\n3,p1,1,10.00\n4,p3,5,3.20\n5,p2,2,oops\n6,p4,1,99.00\n")

# 2. Explicit schema: price is read as a string so a bad value cannot fail the read
schema = StructType([
    StructField("order_id", IntegerType(), False),
    StructField("product_id", StringType(), False),
    StructField("qty", IntegerType(), True),
    StructField("price", StringType(), True),
])
orders = spark.read.csv(path, header=True, schema=schema)

# 3. Transformations (lazy): parse safely, derive revenue, drop rows that failed to parse
clean = (orders
         .withColumn("price", F.expr("try_cast(price AS DECIMAL(10,2))"))
         .withColumn("revenue", F.col("qty") * F.col("price"))
         .filter(F.col("price").isNotNull()))

products = spark.createDataFrame(
    [("p1", "books"), ("p2", "books"), ("p3", "toys"), ("p4", "toys")],
    ["product_id", "category"])

# 4. Join on the column name (no duplicate key column); hint that products is small
enriched = clean.join(F.broadcast(products), "product_id", "left")

per_product = enriched.groupBy("category", "product_id").agg(F.sum("revenue").alias("revenue"))
w = Window.partitionBy("category").orderBy(F.col("revenue").desc())
ranked = per_product.withColumn("rank", F.row_number().over(w))

# 5. Actions: nothing ran until here
rows = ranked.orderBy("category", "rank").collect()
for r in rows:
    print(r.category, r.product_id, str(r.revenue), r.rank)

assert orders.count() == 6 and clean.count() == 5          # 'oops' became NULL and was filtered
assert [(r.category, r.product_id, r.rank) for r in rows] == [
    ("books", "p1", 1), ("books", "p2", 2), ("toys", "p4", 1), ("toys", "p3", 2)]

# 6. Read the physical plan: look for BroadcastHashJoin and Exchange (shuffle) nodes
ranked.explain(mode="formatted")
spark.stop()

What to notice:

  • Steps 3 and 4 return instantly; Spark only builds a plan. The first collect() runs everything, and count() runs the plan again (cache a DataFrame only when several actions reuse it and it is expensive to recompute).
  • try_cast turned oops into NULL. A plain cast would raise an error under ANSI mode, which is often what you want in a strict pipeline; decide deliberately and quarantine bad rows rather than silently dropping them in production.
  • The plan shows a BroadcastHashJoin (no shuffle for the join, because products is sent to every executor) and Exchange nodes for the groupBy and the window’s partitionBy. Counting Exchange nodes is how you count shuffles; the BroadcastExchange under the join is the broadcast, not a shuffle.
  • collect() is fine here because the result is four rows. On real data, write the result to storage instead.

PySpark vs pandas (and other alternatives)

PySpark pandas Polars / DuckDB
Scale Many machines; terabytes One machine; data must fit in memory One machine; larger-than-memory with streaming or out-of-core execution
Execution Lazy, optimised plan, distributed Eager, vectorised operations on one machine, in memory Lazy (Polars lazy API) or SQL with a query optimiser
Startup cost Seconds (JVM, cluster) None None
Best for Large batch and streaming pipelines, lakehouse tables Small data, notebooks, quick analysis Single-node ETL and analytics up to tens or hundreds of gigabytes

The other common question is DataFrame API vs Spark SQL: they compile to the same plans and run at the same speed, so choose by readability and testability. Many teams write SQL for set-based transforms and the DataFrame API for reusable, parameterised logic. The pandas API on Spark exists for porting pandas code, but idiomatic DataFrame code is usually clearer and faster.

What this course covers

  1. PySpark Fundamentals for Data Engineers (this page): the map, core ideas and a runnable example.
  2. PySpark DataFrames, Columns and Schemas: creating DataFrames, column expressions, explicit schemas, parse modes, nulls and ANSI casts.
  3. PySpark Transformations vs Actions: lazy evaluation, narrow and wide transformations, and where jobs and stages come from.
  4. PySpark Aggregations, Pivot and Temporary Views: groupBy, agg, pivots, and mixing SQL with DataFrames.
  5. PySpark Joins and Join Strategy: join types, semi and anti joins, duplicate keys, broadcast versus sort-merge.
  6. PySpark Window Functions: ranking, lag and lead, running totals and frames.
  7. PySpark UDFs, pandas UDFs and the Pandas API on Spark: when custom Python is worth it and how to keep it fast.
  8. Nested and Complex Types in PySpark: structs, arrays, maps, explode and JSON.
  9. Reading and Writing Data in PySpark: Parquet, CSV, JSON, ORC, Avro and JDBC sources and sinks.
  10. Writing Efficient Output: file sizes, small files, partitioned writes and dynamic partition overwrite.

The engine underneath is its own course: Spark architecture, jobs, stages and tasks, partitions, shuffles and skew, Adaptive Query Execution and Structured Streaming. Practise end to end with the large-scale batch processing project and revise with the PySpark cheat sheet.

Checkpoints

Step You are ready to move on when you can…
DataFrames Read a file with an explicit schema and explain the parse mode
Laziness Point to where the shuffles are in a job
Joins Choose between broadcast and sort-merge and explain why
Windows Write top-N-per-group in PySpark
Performance Diagnose skew from the Spark UI

Common beginner mistakes

  1. Calling collect() or toPandas() on large DataFrames. Everything lands on the driver. Aggregate or write to storage instead.
  2. Relying on schema inference in production. Inference reads the data an extra time and can change types between runs. Declare the schema.
  3. Using Python UDFs for things built-in functions already do (string cleanup, date parsing, CASE logic). It is slower and blocks optimisation.
  4. Looping with withColumn in a Python loop over hundreds of columns. Each call adds a plan node; build the column list and use one select.
  5. Leaving spark.sql.shuffle.partitions at 200 for tiny data, or for huge data. With AQE enabled (the default) Spark coalesces small partitions, but very large shuffles may still need a higher setting.
  6. A window with no partitionBy. All rows go to one partition and one task.
  7. Calling count() everywhere for debugging. Each action re-runs the plan from the source.
  8. Writing thousands of tiny files. Control output partitions before writing, and partition by low-cardinality columns only.

What interviewers ask

PySpark rounds combine concepts, live coding and debugging:

  • “What is lazy evaluation, and what is the difference between a transformation and an action?” (Q3, Q4, Transformation vs action)
  • “Narrow vs wide transformations; what causes a shuffle?” (Q5, What causes a shuffle)
  • “repartition vs coalesce?” (Q7)
  • “Keep the latest record per key” and “top N per group” as live coding (Q13, Q37)
  • “When is a broadcast join used, and what are its limits?” (Q20, Broadcast join)
  • “Why avoid Python UDFs?” (Avoid Python UDFs)
  • “A job is slow and one task takes far longer than the others. What do you do?” (Q21, Data skew)
  • “Spark, pandas or Polars?” (Q38)

All 50 are in Top 50 PySpark Interview Questions and Answers. When you answer, mention the plan (explain()), the stage boundaries and the Spark UI evidence you would check; that is what separates working knowledge from memorised definitions.

Key takeaway

PySpark is Python on a distributed, lazily evaluated engine. Declare schemas, prefer built-in functions, know where your shuffles are, keep data off the driver, and read the plan before you tune anything.

By , Data Engineer and Founder of DataDank · Last reviewed Oct 2026 · The worked example runs on PySpark 4.2.0 in local mode (Python 3, Java 17), verified with scripts/verify-examples.py. Linked lessons also run on PySpark 4.2.

Progress is saved in this browser only. No account needed.

Search
Filter by type