liveBook · Manning

Drop_duplicates

As explained in Pandas Workout: 200 exercises to make you a stronger data analyst

A pandas method that removes duplicate rows or values according to the selected subset and retention rules.

Use drop_duplicates when a filtered result contains repeated entries but the question calls for distinct values or rows. Select the identifying columns first, then remove repetitions. section 7.10 section 13.3

Find cities with qualifying rainfall

After grouping weather records by city and state and filtering for groups with repeated heavy-rain readings, select city and state, then call drop_duplicates() to return each qualifying city once. The result identifies New York and Los Angeles rather than one row per qualifying reading. section 7.10

Count undergraduate-only institutions

Compare undergraduate and graduate school names with isin and invert the boolean result with ~. Applying drop_duplicates() to the remaining names produces distinct institutions before size counts them; the reported count is 923. section 13.3

(
    df
    .groupby(['city', 'state'])
    .filter(has_multiple_readings_at_least)
    [['city', 'state']]
    .drop_duplicates()
)

Inspect the complete weather pipeline that filters grouped records, selects city and state, and removes duplicate rows. section 7.10

How Pandas Workout: 200 exercises to make you a stronger data analyst covers it

How deep it goes
Explains practical deduplication in chained pandas expressions but does not detail method parameters or retention behavior.
Examples
Uses rainfall records by city and state plus undergraduate and graduate institution names.
What it assumes
Assumes familiarity with pandas filtering, column selection, grouping, boolean Series, and chained expressions.
What you can do after
Apply it via pandas to produce distinct values or rows before inspecting or counting filtered results.
How it connects
Connects filtering, grouping, column selection, boolean indexing, isin, and size-based counting.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pandas Workout: 200 exercises to make you a stronger data analyst. The section links point to the parts of the book it draws on.