Handling Duplicates & Data Type Conversions
Handling Duplicates & Data Type Conversions
Reading Time: 8 Mins | Level: Beginner to Advanced
1. Detecting & Removing Duplicate Records
Duplicate rows often occur due to repeated form submissions, database re-syncs, or ETL joining errors.
The keep Parameter Options:
keep='first'(Default): Retains the first occurrence and drops subsequent duplicates.keep='last': Retains the last occurrence and drops earlier duplicates.keep=False: Drops ALL occurrences of duplicates (keeping only strictly unique rows).
2. Converting Data Types (.astype())
Often numbers (like "₹4500" or phone numbers "9876543210") are imported as generic object (string) types. To perform calculations, cast them to proper datatypes:
Safe Conversion with pd.to_numeric()
When a column contains dirty strings (like "N/A" or "invalid"), .astype(float) crashes with a ValueError. pd.to_numeric(errors='coerce') gracefully converts bad values into NaN:
3. The Categorical Data Type (category)
For text columns with limited unique values (e.g. Gender, State, Department), converting from object to category reduces memory usage by up to 90% and speeds up grouping:
Multiple Choice Questions
df.drop_duplicates(keep=False)?pd.to_numeric(series, errors='coerce') do when encountering unparseable text?Applying Custom Functions (apply, map, applymap)
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Dropping & Filling Missing Values (dropna, fillna) | Applying Custom Functions (apply, map, applymap) |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.