How to Count the Number of Rows in a PySpark DataFrame: The Ultimate Guide

Published on April 17, 2025


Diving Straight into Counting Rows in a PySpark DataFrame

Need to know how many rows are in your PySpark DataFrame—like customer records or event logs—to validate data or monitor an ETL pipeline? Counting the number of rows in a DataFrame is a core skill for data engineers working with Apache Spark. It provides a quick way to assess dataset size and ensure data integrity. This guide dives into the syntax and steps for counting rows in a PySpark DataFrame, with examples covering essential scenarios. We’ll tackle key errors to keep your pipelines robust. Let’s count those rows! For more on PySpark, see Introduction to PySpark.


Counting the Number of Rows in a DataFrame

The primary method for counting the number of rows in a PySpark DataFrame is the count() method, which returns the total number of rows as an integer. The SparkSession, Spark’s unified entry point, executes this operation across the distributed dataset. This approach is ideal for ETL pipelines needing to verify data volume or monitor processing. Here’s the basic syntax:

from pyspark.sql import SparkSession

spark = SparkSession.builder.appName("CountRows").getOrCreate()
df = spark.createDataFrame(data, schema)
row_count = df.count()

Let’s apply it to an employee DataFrame with IDs, names, ages, and salaries:

from pyspark.sql import SparkSession

# Initialize SparkSession
spark = SparkSession.builder.appName("CountRows").getOrCreate()

# Create DataFrame
data = [
    ("E001", "Alice", 25, 75000.0),
    ("E002", "Bob", 30, 82000.5),
    ("E003", "Cathy", 28, 90000.75),
    ("E004", "David", 35, 100000.25)
]
df = spark.createDataFrame(data, ["employee_id", "name", "age", "salary"])

# Count rows
row_count = df.count()
print(f"Number of rows: {row_count}")

Output:

Number of rows: 4

This counts all rows in the DataFrame. Validate: assert row_count == 4, "Unexpected row count". For SparkSession details, see SparkSession in PySpark.


Counting Rows in a Simple DataFrame

Counting rows in a DataFrame with flat columns, like strings or numbers, is the most common use case for validating dataset size in ETL tasks, such as checking loaded data, as seen in ETL Pipelines. The count() method is straightforward:

from pyspark.sql import SparkSession

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

# Create DataFrame
data = [
    ("E001", "Alice", 25, 75000.0),
    ("E002", "Bob", 30, 82000.5),
    ("E003", "Cathy", 28, 90000.75)
]
df = spark.createDataFrame(data, ["employee_id", "name", "age", "salary"])

# Count rows
row_count = df.count()
print(f"Number of rows: {row_count}")

Output:

Number of rows: 3

This confirms the DataFrame has 3 rows, useful for quick verification. Error to Watch: Empty DataFrame handling:

try:
    empty_df = spark.createDataFrame([], schema=["employee_id", "name"])
    row_count = empty_df.count()
    print(f"Number of rows: {row_count}")
except Exception as e:
    print(f"Error: {e}")

Output:

Number of rows: 0

Fix: Handle empty DataFrames: row_count = df.count(); assert row_count >= 0, "Invalid row count".


Counting Rows in a Nested DataFrame

Nested DataFrames, with structs or arrays, model complex relationships, like employee contact details or project lists, extending simple counts for inspecting advanced ETL data, as discussed in DataFrame UDFs:

from pyspark.sql import SparkSession
from pyspark.sql.types import StructType, StructField, StringType, LongType, ArrayType

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

# Define schema with nested structs and arrays
schema = StructType([
    StructField("employee_id", StringType(), False),
    StructField("name", StringType(), True),
    StructField("contact", StructType([
        StructField("phone", LongType(), True),
        StructField("email", StringType(), True)
    ]), True),
    StructField("projects", ArrayType(StringType()), True)
])

# Create DataFrame
data = [
    ("E001", "Alice", (1234567890, "alice}example.com"), ["Project A", "Project B"]),
    ("E002", "Bob", (9876543210, "bob}example.com"), ["Project C"]),
    ("E003", "Cathy", (None, None), [])
]
df = spark.createDataFrame(data, schema)

# Count rows
row_count = df.count()
print(f"Number of rows: {row_count}")

Output:

Number of rows: 3

This counts rows regardless of nested complexity, aiding validation of structured data. Validate: assert row_count == 3, "Unexpected row count".


Counting Rows with Filtering

Counting rows after applying a filter, like employees with high salaries, is common for analyzing subsets of data in ETL pipelines, extending nested counts for targeted insights, as seen in DataFrame Operations:

from pyspark.sql import SparkSession

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

# Create DataFrame
data = [
    ("E001", "Alice", 25, 75000.0),
    ("E002", "Bob", 30, 82000.5),
    ("E003", "Cathy", 28, 90000.75),
    ("E004", "David", 35, 100000.25)
]
df = spark.createDataFrame(data, ["employee_id", "name", "age", "salary"])

# Filter and count rows
filtered_df = df.filter(df.salary > 80000)
row_count = filtered_df.count()
print(f"Number of rows with salary > 80000: {row_count}")

Output:

Number of rows with salary > 80000: 3

This counts rows meeting the condition, useful for data validation. Error to Watch: Invalid filter conditions fail:

try:
    invalid_df = df.filter(df.invalid_column > 80000)
    row_count = invalid_df.count()
except Exception as e:
    print(f"Error: {e}")

Output:

Error: Column 'invalid_column' does not exist

Fix: Validate columns: assert "salary" in df.columns, "Column missing". Ensure correct filter syntax.


Counting Rows Using SQL Queries

Using a SQL query via a temporary view to count rows provides an alternative approach, extending filtered counts for SQL-based ETL workflows, as seen in DataFrame Operations:

from pyspark.sql import SparkSession

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

# Create DataFrame
data = [
    ("E001", "Alice", 25, 75000.0),
    ("E002", "Bob", 30, 82000.5),
    ("E003", "Cathy", 28, 90000.75)
]
df = spark.createDataFrame(data, ["employee_id", "name", "age", "salary"])

# Create temporary view
df.createOrReplaceTempView("employees")

# Count rows using SQL
result_df = spark.sql("SELECT COUNT(*) AS row_count FROM employees")
row_count = result_df.collect()[0]["row_count"]
print(f"Number of rows: {row_count}")

Output:

Number of rows: 3

This uses SQL for counting, ideal for SQL-driven pipelines. Validate view: assert "employees" in [v.name for v in spark.catalog.listTables()], "View missing".


How to Fix Common Row Counting Errors

Errors can disrupt row counting. Here are key issues, with fixes:

  1. Empty DataFrame: Counting an empty DataFrame returns 0. Fix: Check: row_count = df.count(); assert row_count >= 0, "Invalid row count".
  2. Invalid Filter Conditions: Wrong column names fail. Fix: Validate: assert column in df.columns, "Column missing".
  3. Non-Existent View: SQL on unregistered views fails. Fix: assert view_name in [v.name for v in spark.catalog.listTables()], "View missing". Register: df.createOrReplaceTempView(view_name).

For more, see Error Handling and Debugging.


Wrapping Up Your Row Counting Mastery

Counting the number of rows in a PySpark DataFrame is a vital skill, and Spark’s count() method and SQL queries make it easy to handle simple, nested, filtered, and SQL-based scenarios. These techniques will level up your ETL pipelines. Try them in your next Spark job, and share tips or questions in the comments or on X. Keep exploring with DataFrame Operations!


More Spark Resources to Keep You Going