Dispalying DataFrames

Spark tutorial · PySpark.in

Displaying Data in PySpark

When you first load a DataFrame in PySpark, you need ways to inspect it without loading an entire distributed dataset into memory. Unlike Pandas (which prints a full table), PySpark provides optimized methods that show a sample of the data or return small subsets as Python objects. This section covers the most common techniques— show(), take(), and collect()—along with best practices and use cases.

1. show() – The Most Common Display Method

The show() method prints rows in a clean, tabular format. By default it shows the first 20 rows and truncates long string columns to 20 characters. You can adjust the number of rows and truncation behavior:

Example 1: Display full DataFrame

```

from pyspark.sql import SparkSession

spark = SparkSession.builder.appName("ShowExample").getOrCreate()

data = [(1, "Laptop", 55000, 2), (2, "Mouse", 500, 10), (3, "Keyboard", 1200, 4)]

columns = ["id", "product", "price", "quantity"]

df = spark.createDataFrame(data, columns)

df.show() # default: 20 rows, truncated

df.show(5) # show up to 5 rows

df.show(truncate=False) # show full column values

df.show(5, truncate=False) # show 5 rows with no truncation

df.show(vertical=True) # print each row vertically

```

2. take() – Returns Rows as Python Objects

Use take(n) to retrieve the first n rows as a list of Row objects. According to the API documentation, DataFrame.take(num) returns the first num rows as a list of Row. This is especially useful for programmatic checks, unit tests, or conditional logic where you need direct access to row contents.

Example:

```

from pyspark.sql import SparkSession

spark = SparkSession.builder \

.appName("TakeExample") \

.master("local[*]") \

.getOrCreate()

df = spark.createDataFrame(

[(1,"Apple"),(2,"Banana"),(3,"Grapes")],

["id","fruit"]

)

rows = df.take(2)

for r in rows:

print(r)

```

When to use?

3. collect() – Bring Entire Dataset to Driver

The collect() method returns all records as a list of Row objects. The documentation cautions that this should only be used when the resulting list is small, because it loads the entire DataFrame into the driver’s memory. Avoid calling collect() on large datasets, as it can lead to memory errors or crash the driver.

```

from pyspark.sql import SparkSession

spark = SparkSession.builder \

.appName("CollectExample") \

.master("local[*]") \

.getOrCreate()

df = spark.createDataFrame(

[(10,"X"),(20,"Y"),(30,"Z")],

["score","grade"]

)

all_rows = df.collect()

print(all_rows)

```

Avoid collect() on large datasets

It can cause:

Best Practices

Comparison Table:

Function

Purpose

Returns

When to Use

Show()

Prints sample rows

None (prints to console)

Quick preview

take(n)

Fetches n rows

List of Row objects

Conditional logic, unit tests

Collect()

Fetches all rows

List of Row objects

Small datasets or final results

More Spark tutorials

All tutorials · Try the free PySpark compiler · Practice challenges