Skip to content

Deduplicating and aggregating DataFrames

Deduplication in Spark DataFrames uses dropDuplicates() (optionally on specific columns) or distinct(), and both are wide transformations requiring a shuffle. Aggregation functions like count, approx_count_distinct, mean/avg, and summary/describe let you profile and summarize data, with approx_count_distinct trading exact accuracy for speed on large datasets.

1 · Learn the must-know

  • dropDuplicates() without arguments compares all columns; dropDuplicates(["col1","col2"]) dedupes based on a subset, keeping an arbitrary remaining row per group.
  • distinct() is equivalent to dropDuplicates() with no column arguments, considering the entire row.
  • count() returns the exact number of rows/non-null values and can be expensive on very large datasets since it requires a full scan.
  • approx_count_distinct() uses the HyperLogLog algorithm to estimate distinct counts much faster than countDistinct(), at the cost of a small, configurable error rate (default ~5%).
  • summary() returns count, mean, stddev, min, max, and percentiles (25%, 50%, 75%) by default and accepts custom statistics as arguments, unlike describe() which only gives count, mean, stddev, min, max.
  • groupBy().agg() combined with functions like count, mean, sum, min, max is the standard pattern for computing aggregate statistics per group, and null values are excluded from numeric aggregations like mean by default.

2 · Check your understanding

Check this objectiveFree · always available

A data engineer needs to remove duplicate customer records from customers_df, but only wants to keep the first occurrence based on customer_id, ignoring differences in other columns like last_updated. Which code accomplishes this?

Your objective map0 tried · 0 answered correctly · 33 untouched

What you have tried across Databricks DEA's objectives, not a readiness score.

Databricks Intelligence Platform6% of the exam0 of 2 tried
Data Ingestion and Loading21% of the exam0 of 7 tried
Data Transformation and Modeling22% of the exam0 of 7 tried
Working with Lakeflow Jobs16% of the exam0 of 4 tried
Implementing CI/CD10% of the exam0 of 4 tried
Troubleshooting, Monitoring, and Optimization10% of the exam0 of 5 tried
Governance and Security15% of the exam0 of 4 tried

3 · Keep going