QuestionQ279

VerifiedData Processing & Transformations

A developer is building a data pipeline that processes records from a bronze table into a silver table. The bronze table, bronze_events, contains duplicate records due to at-least-once delivery guarantees from the upstream ingestion system. The developer writes the following PySpark code to prepare the data:

Question Image

After running dropDuplicates() without arguments, the developer notices that some rows that differ only in the event_timestamp column still remain. The developer wants to deduplicate the data based only on the user_id and event_type columns, keeping one row per unique combination of these two columns.

Which code change achieves this deduplication requirement?

Explanation

The dropDuplicates() method accepts an optional subset argument — a list of column names — that determines which columns are used to identify duplicate rows, while the output still retains all original columns of the first matching row. Calling df.dropDuplicates(["user_id", "event_type"]) removes duplicate rows based solely on the combination of user_id and event_type, ignoring differences in event_timestamp or any other column, which is exactly what is required. The other options either don't return the original row data (groupBy().count()), still perform full-row deduplication before dropping columns (distinct().select()), or deduplicate on an unintended set of remaining columns (drop("event_timestamp").dropDuplicates()).

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!