Your first Spark job: SparkSession + DataFrames
The SQL, database, and data-lake arcs are done — now the engine that actually runs the work at scale. PySpark is the Python API for Apache Spark, the distributed engine behind most big data pipelines. Everything in the next four lessons runs through two objects, and today you meet both: a SparkSession, the entry point to Spark, and a DataFrame, a table of rows and named columns — like a pandas DataFrame, except it can be spread across a whole cluster instead of living in one machine's memory.
The code
from pyspark.sql import SparkSession
spark = SparkSession.builder \
.appName("weather-stations") \
.getOrCreate()
data = [
("NYC", 40.71, -74.01),
("Princeton", 40.36, -74.66),
("Boston", 42.36, -71.06),
]
df = spark.createDataFrame(data, ["city", "lat", "lon"])
df.show()
df.printSchema()
SparkSession.builder starts the session: appName() names the job (this is the name you see in the Spark UI), and getOrCreate() reuses an existing session if one is running, or starts a new one. spark.createDataFrame() turns an ordinary Python list into a DataFrame, with the column names you pass as the second argument. df.show() prints the rows, and df.printSchema() prints the column names and their types.
In practice
In a real pipeline you almost never build a DataFrame from a list — you read one from the lake, for example spark.read.parquet("s3://lake/weather_stations/"). Building from a list is for small tests and prototypes, which is exactly why it is the right first step: it isolates the two new objects before file paths, schemas, and cluster config enter the picture. And every later lesson operates on this same object — transformations, aggregations, and joins are all methods you call on a DataFrame.
In your world: a weather-stations table with lat and lon columns is the starting point for everything spatial you will do later — filtering to a bounding box, joining stations to regions, or feeding a model. Today it is just three rows in a list; soon it will be millions of rows read from Parquet, and the code that touches it will look almost the same.
Python bridge: a Spark DataFrame looks like a pandas DataFrame — rows, named columns, show() is roughly head(), printSchema() is roughly dtypes. The difference is the engine underneath: pandas runs in one process on one machine, so it stops scaling at your RAM. Spark splits the DataFrame across workers, so the same code keeps working when the table grows from thousands of rows to billions.
Further reading
PySpark Tutorial: Getting Started with PySpark — DataCamp (a beginner-friendly walkthrough: creating a SparkSession, reading a CSV into a DataFrame, and first explorations)
Try it
Practice question: build a DataFrame called stations with three rows and four columns — station_id, city, lat, lon — for example ("S1", "Newark", 40.74, -74.17), ("S2", "Trenton", 40.22, -74.76), and ("S3", "Atlantic City", 39.36, -74.42). Call stations.show() and stations.printSchema(). What does printSchema() tell you about the column types?
Show answer
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName("stations").getOrCreate()
data = [
("S1", "Newark", 40.74, -74.17),
("S2", "Trenton", 40.22, -74.76),
("S3", "Atlantic City", 39.36, -74.42),
]
stations = spark.createDataFrame(data, ["station_id", "city", "lat", "lon"])
stations.show()
stations.printSchema()
Result: show() prints the three station rows, and printSchema() reports station_id: string, city: string, lat: double, lon: double (all nullable). Spark inferred the types from the Python values — strings became string, floats became double — which is why a stray integer or a mistyped value in a list can silently change a column's type.