PySpark for Practical Data Science
PySpark is easiest to learn by connecting it to concepts you already know from pandas and SQL—then noticing where distributed execution changes the rules.
SparkSession schema select filter withColumn groupBy join window partition Parquet explain
A small PySpark pipeline
from pyspark.sql import SparkSession
from pyspark.sql import functions as F
spark = SparkSession.builder.getOrCreate()
df = spark.read.parquet("events/")
result = (
df.filter(F.col("event") == "purchase")
.groupBy("country")
.agg(
F.sum("amount").alias("revenue"),
F.count("*").alias("orders")
)
)
result.show()The syntax is not the hard part. The important question is:
What physical work will Spark need to perform?
Learn in this order
DataFrame basics
↓
schemas + data types
↓
select/filter/withColumn
↓
groupBy
↓
joins
↓
window functions
↓
read/write Parquet
↓
partitions + shuffle
↓
query plans
↓
performance
Resources
PySpark DataFrame Quickstart:
https://spark.apache.org/docs/latest/api/python/getting_started/quickstart_df.html
PySpark User Guide:
https://spark.apache.org/docs/latest/api/python/user_guide/index.html
Spark SQL Guide:
https://spark.apache.org/docs/latest/sql-programming-guide.html
Databricks DataFrame Tutorial:
https://docs.databricks.com/en/getting-started/dataframes.html
pandas vs. Spark
pandas
DataFrame → usually local/in-memory
Spark
DataFrame → logical distributed dataset
→ optimized plan
→ tasks over partitions
Avoid Python UDFs when equivalent built-in Spark expressions exist; built-in expressions give Spark more opportunity to optimize execution.
DS/ML bridge
A common architecture is:
raw distributed data
↓
Spark cleaning / aggregation / features
↓
training dataset
↓
ML training
↓
batch/stream scoring pipeline
Spark is often the data engineering layer around ML, not necessarily the algorithm that trains every model.
When a Spark job is surprisingly slow, do not only stare at Python code. Inspect the execution plan, partitions, joins, shuffles and data volume.