Menu

PySpark show() – Display PySpark DataFrame Contents in Table

Written by Jagdeesh | 2 min read

One of the essential functions provided by PySpark is the show() method, which displays the contents of a DataFrame in a tabular format. In this blog post, we will delve into the show() function, its usage, and its various options to help you make the most of this powerful tool.

1. Understanding DataFrames in PySpark

Before we discuss the show() function, it’s essential to understand DataFrames in PySpark. A DataFrame is a distributed collection of data organized into named columns.

It is conceptually equivalent to a table in a relational database or a data frame in R or Python, but optimized for large-scale processing. You can think of a DataFrame as a spreadsheet with rows and columns.

2. PySpark show() Function

The show() function is a method available for DataFrames in PySpark. It is used to display the contents of a DataFrame in a tabular format, making it easier to visualize and understand the data.

This function is particularly useful during the data exploration and debugging phases of a project.

Syntax

DataFrame.show(n=20, truncate=True, vertical=False)

Parameters:

n: The number of rows to display. The default value is 20.

truncate: If set to True, the column content will be truncated if it is too long. The default value is True.

vertical: If set to True, the output will be displayed vertically. The default value is False.

3. Usage of PySpark show()

Let’s look at some examples of how to use the show() function in PySpark

a. Basic Usage

python
import findspark
findspark.init()

from pyspark.sql import SparkSession

spark = SparkSession.builder \
    .appName("PySpark show() Example") \
    .getOrCreate()

data = [("Alice", 34), ("Bob", 45), ("Charlie", 29), ("David", 31)]
columns = ["Name", "Age"]

df = spark.createDataFrame(data, columns)

df.show()
python
+-------+---+
|   Name|Age|
+-------+---+
|  Alice| 34|
|    Bob| 45|
|Charlie| 29|
|  David| 31|
+-------+---+

b. Display a Specific Number of Rows

python
df.show(2)
python
+-----+---+
| Name|Age|
+-----+---+
|Alice| 34|
|  Bob| 45|
+-----+---+
only showing top 2 rows

c. Display Contents Without Truncation

python
df.show(truncate=False)
python
+-------+---+
|Name   |Age|
+-------+---+
|Alice  |34 |
|Bob    |45 |
|Charlie|29 |
|David  |31 |
+-------+---+

d. Display Contents Vertically

python
df.show(vertical=True)
python
-RECORD 0-------
 Name | Alice   
 Age  | 34      
-RECORD 1-------
 Name | Bob     
 Age  | 45      
-RECORD 2-------
 Name | Charlie 
 Age  | 29      
-RECORD 3-------
 Name | David   
 Age  | 31      

Conclusion

The PySpark show() function is a valuable tool for displaying DataFrame contents in a tabular format

Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Jagdeesh
Written by
Related Course
Master PySpark — Hands-On
Join 5,000+ students at edu.machinelearningplus.com
Explore Course
Get the full course,
completely free.
Join 57,000+ students learning Python, SQL & ML. One year of access, all resources included.
📚 10 Courses
🐍 Python & ML
🗄️ SQL
📦 Downloads
📅 1 Year Access
No thanks
🎓
Free AI/ML Starter Kit
Python · SQL · ML · 10 Courses · 57,000+ students
🎉   You're in! Check your inbox (or Promotions/Spam) for the access link.
⚡ Before you go

Python.
SQL. NumPy.
All free.

Get the exact 10-course programming foundation that Data Science professionals use.

🐍
Core Python — from first line to expert level
📈
NumPy & Pandas — the #1 libraries every DS job needs
🗃️
SQL Levels I–III — basics to Window Functions
📄
Real industry data — Jupyter notebooks included
R A M S K
57,000+ students
★★★★★ Rated 4.9/5
⚡ Before you go
Python. SQL.
All Free.
R A M S K
57,000+ students  ★★★★★ 4.9/5
Get Free Access Now
10 courses. Real projects. Zero cost. No credit card.
New learners enrolling right now
🔒 100% free ☕ No spam, ever ✓ Instant access
🚀
You're in!
Check your inbox for your access link.
(Check Promotions or Spam if you don't see it)
Or start your first course right now:
Start Free Course →
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science