Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
Databricks · Databricks Spark Developer Associate

Duplicates, Validation, and Aggregates

Databricks Spark Developer Associate · Lesson 9 of 18 · 1:08:52

YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗

Learning with these videos? Subscribe for new courses and exam-style questions.

In this lesson

Prepare for the Databricks Certified Associate Developer for Apache Spark exam. In this lesson: distinct and dropDuplicates, dropna, fillna and validation rules, counting rows and values, approx_count_distinct, aggregates, describe, and summary. Includes three original practice questions.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

Resent events, missing values

When a phone loses its connection, the Coralpeak game sends the same event again, and some events reach the platform without a player. The live operations team still wants exact revenue, the number of daily players, and a quick profile of every purchase. This lesson covers removing duplicates, validating the data, counting rows and values, and aggregates and summaries.

Removing duplicates

distinct returns the rows that are unique across every column, so two rows are duplicates only if all their values match. dropDuplicates takes a list of columns, and keeps one row for each combination of their values, even when the other columns differ. Spark does not promise which of the duplicate rows it keeps, so when you need a specific one, such as the latest, use an aggregate instead. drop_duplicates, with an underscore, is simply another name for dropDuplicates, and without any columns, it behaves like distinct. Both are wide transformations, since copies of the same row can sit in different partitions, so they trigger a shuffle.

Validating the data

Validation checks that the data follows your rules before it reaches a report, and missing values are the first thing to handle. dropna removes the rows with a null in any column by default, while setting how to all removes only the rows where every column is null. thresh keeps the rows with at least that number of values that are not null, and replaces the how parameter, while subset limits the check to some columns. fillna replaces the nulls with a value instead, such as zero for a missing amount. For business rules, like a negative amount or a missing country, filter the rows that break the rule, count them, and either set them aside in a quarantine table, or stop the job.

Counting rows and values

count on a DataFrame is an action that returns the number of rows, while the count function, inside an aggregation, counts the values that are not null. count_distinct gives the exact number of different values, but Spark must gather every distinct value, which is costly on billions of rows. approx_count_distinct gives an estimate of that number with the HyperLogLog plus plus algorithm, which uses far less memory, and runs much faster on large data. An optional parameter sets the maximum relative standard deviation, five percent by default, and below one percent, the exact count_distinct is the better choice.

Aggregates and summaries

groupBy, followed by agg, computes aggregates for each group, with functions such as sum, min, max, and avg, where mean is simply another name for avg. Called on the whole DataFrame, without groupBy, agg returns a single row of totals. describe returns basic statistics for each column: the count, the mean, the standard deviation, the minimum, and the maximum. summary returns the same statistics, plus the approximate twenty fifth, fiftieth, and seventy fifth percentiles, and you can choose which statistics it computes. Both return a small DataFrame, meant for exploring the data, so you call show to read it.

Recap

Let's recap. distinct removes the rows that match on every column, and dropDuplicates keeps one row for each value of the listed columns. dropna removes the rows with nulls, with how, thresh, and subset, and fillna replaces the nulls. Validation rules are filters: count the rows that break them, then set them aside, or stop the job. count on a DataFrame counts the rows, and approx_count_distinct gives an estimate of the distinct values, far faster than an exact count. And summary adds the approximate percentiles to the statistics of describe.

Quick checks for this lesson

The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.

  1. The game resends some events, with the same event_id but a different arrival time. The team wants one row for each event_id. What should the developer call?

    • distinct()
    • dropDuplicates(["event_id"])
    • dropna(subset=["event_id"])
    • groupBy("event_id").count()
    Show the answer

    dropDuplicates(["event_id"])

    DropDuplicates on the event ID keeps one row for each event, even though the arrival times differ. “distinct()” is wrong, since the arrival times differ, so distinct sees different rows, and keeps them all. “dropna(subset=["event_id"])” only removes rows where the event ID is null. And “groupBy("event_id").count()” would count the copies of each event, without removing any.

  2. A dashboard shows the number of different players each day, from billions of events. It must refresh quickly, and a small error is fine. Which choice fits?

    • count_distinct on player_id
    • count() on the whole DataFrame
    • distinct().count() on all the columns
    • approx_count_distinct on player_id
    Show the answer

    approx_count_distinct on player_id

    Approx_count_distinct gives a close estimate, with far less memory and time. “count_distinct on player_id” is exact, but slower and more costly on billions of events. “count() on the whole DataFrame” would count every event, not the players. And “distinct().count() on all the columns” would count the distinct rows, not the distinct players.

  3. An analyst wants the count, mean, standard deviation, min, max, and the approximate median of the purchase amounts, in one call. Which method fits?

    • summary()
    • describe()
    • printSchema()
    • agg(avg("amount"))
    Show the answer

    summary()

    Summary adds the approximate percentiles, and the fiftieth one is the median. “describe()” is wrong, since describe stops at the minimum and the maximum, with no percentiles. “printSchema()” only prints the column names and types. And “agg(avg("amount"))” would only return the averages, not the other statistics.