Sub topic 4
Looking at your data
This chapter covers the three ways to inspect a DataFrame in a Databricks notebook: printSchema, show, and display. It assumes you can attach a notebook to compute and that you know what a DataFrame is. It also assumes the notebook already provides a spark session.
Build a small DataFrame
The examples use a small in-memory DataFrame. The schema is a DDL-style string. The date column starts as a string and is then converted to a date.
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()
display(orders)Check the schema first
printSchema() prints column names, types, and nullability as a tree. It reads only metadata, so it is cheap and runs at once. Check it before anything else. A date that is still a string is a common source of later errors.
For this DataFrame the output looks like this:
root
|-- order_id: integer (nullable = true)
|-- customer: string (nullable = true)
|-- order_date: date (nullable = true)
|-- amount: double (nullable = true)Look at rows
Two calls show rows. Both are actions, so they make Spark run the query.
| Call | Where it renders | Typical use |
|---|---|---|
| `df.show(n)` | Plain text in the cell output | Quick checks, logs, and non-notebook scripts |
| `display(df)` | Interactive table | Sorting, scrolling, and charting in a notebook |
show() prints 20 rows by default and cuts long values at 20 characters. Pass truncate=False to see full values. Pass a number to change the row count.
orders.show(5, truncate=False)display(df) is a Databricks function, so it is not available in plain PySpark. It renders an interactive table. You can sort and filter the table, and you can add a chart from the result without more code.
Keep inspection cheap
Spark evaluates lazily. A later chapter covers this in detail. For now, know that show and display run the whole query, up to the number of rows they need.
- Do not call
displayon a very large DataFrame you have not narrowed first. It limits the rows it renders, but the work behind it can still be large. - Use
limit(n)to cap the rows when the query is expensive. - Use
count()only when you need the exact number. It scans all the data.
orders.limit(2).show()Note that limit without an orderBy returns an arbitrary set of rows. Do not rely on which ones you get.
Exercise
A colleague runs display(df) on a table with billions of rows after several joins, and the cell runs for minutes. Name one change that makes the inspection cheaper, and say what to check first.
Show answer
Check the schema with printSchema(), which does not run a job. Then reduce the work before you display, for example by filtering rows or calling df.limit(20). limit caps the rows you look at, but without orderBy the rows it returns are arbitrary.