Get the top N rows per group in PySpark
Select the top two rows per group with row_number, make tied results repeatable and choose when rank should include extra rows.
Practical guides to Python, data and AI—with examples you can use.
PYSPARK FOR PYTHON DEVELOPERS
Start with the free guide to understand Spark, DataFrames and lazy execution. When you are ready to build something, the Starter Kit gives you the code, data, tests and exercises.
No subscription. The kit code is tested with PySpark 4.2.0.
reading sales.csv joining customers.csv writing output/enriched_sales transaction_id customer gross 101 Alice 275.55 102 Bob 126.00 103 Charlie 475.82 104 David 95.72 4 valid transactions
START HERE
Understand the mental model before worrying about cluster tuning.
The guide explains what Spark does, how a local cluster works, why transformations are lazy, and how to create and process your first DataFrame.
Free PDF. No payment details required. Your email is not shared or sold.
WHEN YOU WANT TO BUILD IT
A small working project that reads messy CSV input, applies explicit schemas, removes incomplete and duplicate sales, joins customer data, calculates totals and writes Parquet.
pyspark-starter-kit/ |-- guides/ |-- data/ |-- notebooks/ |-- src/pipeline.py |-- tests/ |-- exercises/ |-- solutions/ `-- cheatsheets/
FREE TUTORIALS
Select the top two rows per group with row_number, make tied results repeatable and choose when rank should include extra rows.
Turn array elements into rows, retain empty arrays with explode_outer and pair two arrays without multiplying their rows.
Capture CSV parsing errors with _corrupt_record, check missing and invalid values, and write accepted and rejected records separately.
Diagnose PySpark JAVA_GATEWAY_EXITED by checking the Java process, JAVA_HOME and your Python environment, then verify a local Spark session.
Find the Python interpreter behind a PySpark import error, install into the correct virtual environment and select it in VS Code or a notebook.
Run a PySpark driver under the VS Code Python debugger, set a breakpoint, inspect a row count and distinguish driver code from Spark worker execution.
Install PySpark on Windows, WSL or Ubuntu, connect it to VS Code, then verify a real local session.
Define a schema and make malformed input fail where you can diagnose it.
Build transformations, trigger an action and inspect the execution plan.
Write typed columnar output and read it back without inferring the schema again.
Use dropDuplicates() with a business key and check which records remain.
Apply dropna() to required columns without discarding useful optional data.
Join sales to customer data while retaining transactions without a lookup match.
Create one Spark session, collect a small result and assert the rows that matter.
PAID TUTORIALS
Each guide shows its length and exactly what it teaches.
9-page PDF. Build a local agent with Python, Ollama and SQLite, follow its first tool call and diagnose incorrect answers.
View guide9-page PDF. Extend Part 1 with three database tools, conversation history, execution traces and a bounded agent loop.
View guide10-page PDF. Build on Parts 1 and 2 with argument validation, read-only tools, confirmation before writes and pytest checks.
View guide5-page PDF + Python code. Measure repeated lookups with cProfile, inspect saved profiles and verify an indexed implementation.
View guide6-page PDF + Python tests. Generate nested inputs and check round trips, reference results and invariants with Hypothesis.
View guide6-page PDF + code and CSV. Compare eight Pandas and DuckDB operations, including joins, running totals and Parquet output.
View guide6-page PDF + app and tests. Build a local to-do API with validation, filtering, pagination and request-level checks.
View guide6-page PDF + app, CSV and tests. Build a local dashboard with consistent filters, sales metrics, charts and empty-state handling.
View guide6-page PDF + runnable SQL. Use CTEs, compare ranking functions, select top-N rows and walk a small hierarchy with SQLite.
View guide11 pages. Build a GitHub Actions workflow for a Python project and run Pylint whenever code is pushed.
View guide15 pages. Add automated pytest checks to the pipeline, then deploy the tested output to an AWS S3 bucket.
View guide8 pages. Use grouping sets, rollup and cube to produce subtotals and multi-level reporting results in SQL.
View guide15 pages. See how the Agent Toolkit can plan, reason about and automate AWS-focused development work.
View guide10 pages. Publish a local app with GitHub Pages, here.now or Hugging Face Spaces and compare when each route fits.
View guide6 pages. Write structured and JSON-style instructions for more precise edits in Nano Banana Pro.
View guide