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
