Displaying & Exploring DataFrames
Spark tutorial · PySpark.in
DATAFRAME BASICS • CHAPTER 2 | PYSPARK.IN CURRICULUM Displaying & Exploring DataFrames in PySpark show(), display(), head(), take(), printSchema(), describe() vs. summary() & Preventing collect() Driver OOM |
1. Viewing Rows Safely: show(), display(), head() & take()
Because a PySpark DataFrame can contain billions of rows across a cluster, you should never print the entire DataFrame. Instead, PySpark provides lightweight preview actions that fetch only a small sample back to the Driver:
Method / Property | Type | What It Returns & Default Behavior |
df.show(n=20, truncate=True, vertical=False) | Action | Prints top n rows (default 20) as an ASCII table. Long strings (>20 chars) are cut off unless truncate=False. Returns None! |
df.display() / display(df) | Databricks Action | Databricks-only interactive HTML table with built-in sorting, filtering, charting, and CSV download (not in open-source Spark). |
df.head(n) / df.take(n) | Action | Returns the first n records as a Python List of Row objects (safe for grabbing 1–10 values into Python variables). |
df.first() | Action | Shortcut for df.head(1)[0] — returns the very first Row object. |
df.columns / df.dtypes | Metadata Property | Instant metadata lookup (Zero Spark Job!). Returns list of column names or (col_name, data_type) tuples. |
df.printSchema() | Metadata Method | Prints a tree view of column names, data types, and nullable flags. Does NOT show row counts (unlike Pandas df.info()). |
df.count() | Action | Triggers a distributed Spark job to return the exact total number of rows across all partitions. |
2. Production Danger: Why collect() Crashes Your Driver Node
One of the most expensive mistakes beginners make when transitioning from Pandas is calling df.collect() on large tables. While show(20) and take(5) read only the first partition until they have enough rows, collect() forces every Executor to serialize and transfer 100% of the dataset into the single Driver node's RAM.

Figure 1: Why show()/take() stay fast and safe while collect() triggers Driver OutOfMemory (OOM) crashes (High-Res Zoomable PNG).
3. Statistical Profiling: describe() vs. summary()
Both df.describe() and df.summary() compute summary statistics across numeric and string columns, returning a new DataFrame:
- df.describe(): Computes only 5 basic metrics — count, mean, stddev, min, max. It deliberately excludes percentiles (25%, 50%, 75%) because computing exact or approximate quantiles across distributed partitions requires heavier aggregation and memory overhead.
- df.summary(): Computes everything in describe() PLUS 25%, 50% (median), and 75% approximate percentiles. You can also pass specific metrics like df.summary('count', 'min', '50%', 'max') to save compute time.
4. Runnable PySpark Code: Full DataFrame Exploration Toolkit
Snippet 1: Exploring Structure, Custom show() Formatting, describe() vs. summary() ``` from pyspark.sql import SparkSession ``` |
EXPECTED OUTPUT: ``` hide Columns : ['txn_id', 'gender', 'age', 'category', 'amount'] ``` |
Common Beginner Anti-Pattern: Never Assign .show() to a Variable! WRONG: rdf = df.select(['txn_id', 'amount']).show() |
5. MNC Interview Questions (Chapter 2)
Q1. What is the difference between df.show(), df.take(n), and df.collect()?
Answer: df.show() formats and prints the top 20 rows to stdout and returns None. df.take(n) safely brings only the first n rows back to the Driver as a Python list of Row objects. df.collect() brings ALL rows from the entire cluster into Driver RAM—causing OutOfMemory crashes on big datasets.
Q2. Why does df.describe() not include 25%, 50%, and 75% percentiles like Pandas does?
Answer: Computing basic stats (count, mean, min, max) is cheap and easily parallelizable across partitions. Calculating percentiles requires extra memory/quantile estimation across distributed nodes. Spark keeps describe() lightweight and provides summary() when approximate quantiles are explicitly needed.
Q3. Does df.printSchema() or df.columns trigger a Spark Job?
Answer: No. Both read structural metadata directly from the Driver's Catalyst Logical Plan without scanning a single row of data on Executors.
More Spark tutorials
- Spark SQL Tutorial: Run SQL Queries in PySpark
- Spark SQL Queries in PySpark: spark.sql() Examples
- Registering UDFs in SQL
- PySpark Joins
- Broadcast Join and Skew Join in PySpark
- PySpark SQL Functions: col, lit, when (CASE WHEN)
PySpark tutorial: all lessons · All tutorials · Try the free PySpark compiler · Practice challenges · Beginner tutorial: learn PySpark step by step