PySpark functions
38 in-depth lessons. One function per lesson: what it does, how it works step by step, the gotchas, and every example in PySpark with its SQL equivalent.
Bronze
Module 1: Read and select
Load a dataset, pick and compute columns, filter and sort rows.
- Reading and writing data · Bronze · 8 min read. Load CSV, JSON and Parquet with the right options and schema, and write with the right save mode and layout.
- select · Bronze · 6 min read. Choose, rename and compute columns, and why select beats a chain of withColumn calls.
- filter / where · Bronze · 5 min read. Keep the rows that match a condition, and the null and operator traps that silently drop data.
- withColumn · Bronze · 6 min read. Add or replace one column, and why calling it in a long loop slows Spark down.
- orderBy · Bronze · 4 min read. Sort rows ascending or descending, control where nulls go, and know what a global sort costs.
Module 2: Clean and type
Fix types, strings, dates and nulls.
- cast and schemas · Bronze · 5 min read. Change column types safely, read a schema, and avoid the silent nulls bad casts produce.
- String functions · Bronze · 5 min read. concat vs concat_ws, substring, split, trim and padding, with the null traps in each.
- Parsing dates and timestamps · Bronze · 4 min read. to_date, to_timestamp and date_format patterns, and why yyyy and YYYY are not the same.
- when / otherwise · Bronze · 4 min read. CASE WHEN logic inside a column: first match wins, and a missing otherwise means null.
- fillna / dropna · Bronze · 4 min read. Fill or drop missing values per column, and why fillna(0) leaves string columns alone.
Module 3: Combine and summarise
Aggregate, remove duplicates and join tables, in DataFrames or in SQL.
- groupBy + agg · Bronze · 4 min read. Aggregate rows per group: counts, sums, averages, and how count(*) differs from count(col).
- dropDuplicates · Bronze · 5 min read. Remove duplicate rows or keys, and why "keep the latest row" needs a window instead.
- join · Bronze · 5 min read. Combine DataFrames on a key: inner, left, right and full joins, and the duplicate-key fan-out trap.
- spark.sql and temp views · Bronze · 6 min read. Mix SQL and DataFrames: register views, run spark.sql, and pass parameters safely.
Silver
Module 4: Window functions
Rank rows and compare each row with the one before or after it.
- row_number / rank · Silver · 4 min read. Number and rank rows inside a window, and how the three ranking functions treat ties.
- lag / lead · Silver · 4 min read. Read the previous or next row in a window to compute changes, gaps and streaks.
Module 5: Smarter aggregates
Return the value from the row with the max, and compute medians and percentiles at scale.
- max_by / min_by · Silver · 4 min read. Return the value from the row where another column is largest or smallest, in one aggregate.
- percentile_approx · Silver · 4 min read. Medians and p90s that scale to billions of rows, and how the accuracy setting trades memory for error.
Module 6: Arrays and nested data
Explode arrays (with positions), collect and query them, and flatten JSON.
- explode · Silver · 4 min read. Turn each array element into its own row, and the rows explode silently drops.
- posexplode · Silver · 3 min read. Explode an array and keep each element position, plus the _outer variant that keeps empty arrays.
- collect_list / collect_set · Silver · 4 min read. Gather the values of a group into an array, with or without duplicates, and in which order.
- Array functions · Silver · 4 min read. array_contains, size, element_at, sort_array and the set functions, without exploding.
- JSON and structs · Silver · 6 min read. Parse JSON strings with from_json, reach into nested fields, and flatten structs.
Module 7: Reshape and refine
Pivot tables, filter by another table, match nulls safely, and handle dates and text precisely.
- coalesce · Silver · 4 min read. Take the first non-null value from several columns, and why it is not the same as fillna.
- left_semi / left_anti · Silver · 4 min read. Keep the rows that have a match, or the rows that do not, without duplicating anything.
- eqNullSafe (<=>) · Silver · 4 min read. Equality where null matches null: the fix for joins and comparisons that silently drop null keys.
- pivot · Silver · 5 min read. Turn row values into columns, and why passing the values list up front makes it faster.
- trunc / add_months · Silver · 4 min read. Truncate dates to the month or year and shift them by whole months, including month-end rules.
- Regular expressions from zero · Silver · 8 min read. What a regex is, the dozen symbols behind almost every pattern, and how to read and build one piece by piece.
- regexp_extract · Silver · 8 min read. Extract, filter, replace and collect with a regular expression, and what you get when nothing matches.
Module 8: Beyond built-ins
Write a UDF only when you must, and test your transformations.
- UDFs and pandas UDFs · Silver · 7 min read. When a Python function is the only way, how to write it, and why built-ins are much faster.
- Testing PySpark code · Silver · 6 min read. Unit test transformations with a local SparkSession and assertDataFrameEqual, and the cases worth covering.
Gold
Module 9: Gold window patterns
Write value-based frames, forward fills, streaks and sessions.
- rangeBetween · Gold · 6 min read. Window frames by value instead of row count: the last 7 calendar days, not the last 7 rows.
- last(ignorenulls) · Gold · 4 min read. Forward-fill missing values with last over a window, and the frame that makes it work.
- Gaps and islands · Gold · 7 min read. Streaks, sessions and status runs: one window pattern behind many interview questions.
Module 10: Gold reshaping patterns
Build subtotals in one pass, turn wide tables long, and combine tables whose schemas drift.
- rollup / cube · Gold · 4 min read. Subtotals and grand totals in one pass, and how grouping() tells a total row from a real null.
- unpivot · Gold · 3 min read. Turn columns into rows (wide to long) with unpivot or the SQL stack() function.
- unionByName · Gold · 4 min read. Stack DataFrames by column name instead of position, and survive schema drift with allowMissingColumns.