Sub topic 3
What a DataFrame is
This chapter assumes you can read basic Python or SQL, understand tabular data (rows, columns, types), and can attach a Databricks notebook to compute. It also assumes you know that a notebook has a preconfigured spark session, which the previous chapter covered.
The working model
A Spark DataFrame is a distributed table with named, typed columns. You describe what you want to do with it. Spark decides how to run that work across the machines in your compute.
Three properties explain most of its behavior:
- Columns have names and types. Each column has a data type such as
INT,STRING,DATEorDOUBLE. The list of columns and types is the schema. - Rows are split into partitions. The data is divided into chunks that different executors process in parallel. You rarely handle partitions directly, but they explain why some operations are cheap and others are not.
- DataFrames are immutable. Methods such as
filterorwithColumnnever change a DataFrame. They return a new one that describes the changed result.
The spark session is the entry point. It creates DataFrames, whether from in-memory rows, files or tables.
Creating a small DataFrame
The fastest way to get a DataFrame is to build one from rows in memory. The second argument is a schema written as a DDL string, which is the same syntax as the column list in a SQL CREATE TABLE.
from pyspark.sql import functions as F
orders = spark.createDataFrame(
[
(1, "alice", "2024-01-05", 120.0),
(2, "bob", "2024-01-06", 75.5),
(3, "alice", "2024-02-10", 30.0),
(4, "carol", "2024-02-11", None),
],
"order_id INT, customer STRING, order_date STRING, amount DOUBLE",
)
orders = orders.withColumn("order_date", F.to_date("order_date"))
orders.printSchema()The dates arrive as strings, so F.to_date converts that column to a real DATE. The assignment orders = orders.withColumn(...) works because withColumn returns a new DataFrame. If you leave out the assignment, the original stays unchanged.
printSchema() prints the column names, types and whether each column allows nulls. Carol's amount is None, which becomes a null in a DOUBLE column.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| A transformation seems to have no effect | The result was not assigned to a variable | Assign the returned DataFrame |
| Date comparisons give odd results | The column is still a `STRING` | Convert it with `F.to_date` |
| `AnalysisException: cannot resolve column` | The column name is misspelled or was dropped earlier | Check `orders.columns` |
| Rows look like text in a numeric column | The type was wrong when the data was loaded | Set the schema explicitly |
Exercise
You run orders.filter(F.col("amount") > 50) on its own line and then print orders.columns. A colleague says the filter removed rows from orders. Is that right, and why?
Show answer
No. DataFrames are immutable. filter returns a new DataFrame and leaves orders unchanged. To keep the filtered result, assign it, for example big_orders = orders.filter(F.col("amount") > 50).