home
diamond Go Premium
Data Engineering Path  ·  PySpark

DataFrame Distinct & Deduplication Operations

Deduplicating rows in a DataFrame using distinct and column-specific dropDuplicates in PySpark.


What is the Deduplication Operation?

Deduplication removes duplicate row records from a dataset, which is a vital data quality step in ET pipelines.

In PySpark, we have two primary operations to achieve this:

  • distinct(): Removes rows only if the entire row (all columns combined) is identical to another row. Equivalent to SELECT DISTINCT in SQL.
  • dropDuplicates(subsetColumns): Removes rows based on uniqueness of a subset of specific columns, keeping the first encountered row and discarding duplicates. This is extremely powerful for transactional data!

Syntax and Subset Filtering

# A. Remove absolute duplicates across all columns
df.distinct()

# B. Remove duplicates based on ID only
# Keeps the first record matching an id and discards subsequent duplicates
df.dropDuplicates(["user_id"])

# C. Multi-column subset uniqueness
df.dropDuplicates(["user_id", "transaction_date"])

Example Usage Pipeline

Below is a complete, copy-paste-ready PySpark script demonstrating deduplication:

from pyspark.sql import SparkSession
from pyspark.sql import functions as F

# 1. Setup local Spark session
spark = SparkSession.builder \
    .appName("DataFrame Deduplication Demo") \
    .master("local[*]") \
    .getOrCreate()

# 2. Dummy dataset with duplicates
# user 1 logged in twice. user 2 has two identical records.
data = [
    ("usr_1", "2026-05-30 10:00:00", "ACTIVE"),
    ("usr_1", "2026-05-30 10:05:00", "ACTIVE"),  # Low-level duplicate (different time)
    ("usr_2", "2026-05-30 11:00:00", "ACTIVE"),
    ("usr_2", "2026-05-30 11:00:00", "ACTIVE"),  # Absolute exact duplicate
    ("usr_3", "2026-05-30 12:00:00", "BLOCKED"),
]
columns = ["user_id", "login_time", "status"]
df = spark.createDataFrame(data, columns)

# 3. Apply distinct() (only removes the absolute identical user 2 record)
distinct_rows = df.distinct()

# 4. Apply dropDuplicates() on 'user id' subset 
# Keeps only the first occurrence for each user id, regardless of login time
unique_users = df.dropDuplicates(["user_id"])

# 5. Show results
print("=== Original Transactions (with Duplicates) ===")
df.show(truncate=False)

print("=== distinct() Output (Removes absolute exact duplicates only) ===")
distinct_rows.show(truncate=False)

print("=== dropDuplicates(['user_id']) Output (Deduplicates by ID) ===")
unique_users.show(truncate=False)

Rendered Output:

=== Original Transactions (with Duplicates) ===
+-------+-------------------+---------+
|user_id|login_time         |status   |
+-------+-------------------+---------+
|usr_1  |2026-05-30 10:00:00|ACTIVE   |
|usr_1  |2026-05-30 10:05:00|ACTIVE   |
|usr_2  |2026-05-30 11:00:00|ACTIVE   |
|usr_2  |2026-05-30 11:00:00|ACTIVE   |
|usr_3  |2026-05-30 12:00:00|BLOCKED  |
+-------+-------------------+---------+

=== distinct() Output (Removes absolute exact duplicates only) ===
+-------+-------------------+---------+
|user_id|login_time         |status   |
+-------+-------------------+---------+
|usr_1  |2026-05-30 10:00:00|ACTIVE   |
|usr_1  |2026-05-30 10:05:00|ACTIVE   |
|usr_2  |2026-05-30 11:00:00|ACTIVE   |
|usr_3  |2026-05-30 12:00:00|BLOCKED  |
+-------+-------------------+---------+

=== dropDuplicates(['user_id']) Output (Deduplicates by ID) ===
+-------+-------------------+---------+
|user_id|login_time         |status   |
+-------+-------------------+---------+
|usr_1  |2026-05-30 10:00:00|ACTIVE   |
|usr_2  |2026-05-30 11:00:00|ACTIVE   |
|usr_3  |2026-05-30 12:00:00|BLOCKED  |
+-------+-------------------+---------+
Find this content helpful? ☕ Buy me a coffee

Entity Details

Create New Item

celebration
Enjoying the free content?

Create a free account to track your progress and save your place.

Create Free Account
help

Submit Technical Query

Have a question or run into an issue? Describe it below, upload an optional screenshot, and our engineering team will answer it!

image Attach image (optional)

Submit Feedback

build Free Developer Utility Free Tool
gavel

Privacy & Legal Disclaimer

1. Client-Side Browser Processing

All utility tools on DeepEngineerHub (including Image to PDF, Text Formatters, JSON Converters, and Encryptors) execute 100% locally within your client browser using WebAssembly and JavaScript. No uploaded images, text, or documents are transmitted, collected, or stored on remote servers.

2. Limitation of Liability ("As-Is" Provision)

Tools and services are provided free of charge for convenience and educational purposes "as-is" without warranties of any kind. DeepEngineerHub shall not be held liable for any data loss, formatting inconsistencies, or indirect damages resulting from tool usage.

3. Open Source & Third-Party Software

Certain utilities utilize open-source client libraries (such as jsPDF, Mermaid.js, Pyodide) licensed under MIT, Apache, or BSD open licenses. All intellectual property remains with their respective copyright holders.