Fix dtypes and Duplicate Rows
Coercing types and dropping repeats.
Fix dtypes and Duplicate Rows is a free Data Science Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Data Science Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Clean Goes Beyond Gaps
Filling missing values is only half the job. Clean data also needs correct dtypes and no accidental duplicate rows muddying your counts. 🧹
What a dtype Is
Every column has a dtype, its data type, like int, float, object, or datetime. The dtype controls which operations the column even allows.
df.dtypesNumbers Stuck as Text
A frequent mess: numbers loaded as object strings because one stray symbol crept in. You cannot do math on them until the type is fixed.
Coerce With to_numeric
Convert a text column to numbers with to_numeric. Pass errors='coerce' to turn anything unparseable into NaN instead of crashing.
df['price'] = pd.to_numeric(df['price'], errors='coerce')Cast With astype
When a column is already clean, astype changes its type directly, for example turning whole-number floats back into compact integers.
df['count'] = df['count'].astype(int)Fix Date Columns
Dates often arrive as text. Convert them with to_datetime so you can sort, filter by range, and extract parts like month later.
df['date'] = pd.to_datetime(df['date'])Category to Save Memory
Columns with few repeated text values shrink dramatically as the category dtype, which stores each label once and references it.
df['city'] = df['city'].astype('category')Spotting Duplicate Rows
Repeated records inflate totals and averages. Find them with duplicated(), which flags each row that has appeared before as True.
df.duplicated().sum()Dropping Duplicates
Remove repeats with drop_duplicates(). By default it keeps the first occurrence of each row and discards the rest.
df = df.drop_duplicates()Duplicates by Key Columns
Sometimes only certain columns define a duplicate. Pass subset to detect repeats based on a key like an id, ignoring the other columns.
df.drop_duplicates(subset=['user_id'])Verify Before You Move On
After fixing types and repeats, run info() once more. Confirming dtypes and row counts protects you from cleaning errors slipping downstream.
df.info()Quick Check
A price column loaded as object text. Which call safely turns bad entries into NaN?
Recap: Types and Dupes Tamed
You now fix dtypes with to_numeric, astype, and to_datetime, and clear repeats with drop_duplicates. Your table is finally analysis-ready.
Frequently asked questions
Is the “Fix dtypes and Duplicate Rows” lesson free?
Yes — the full text of “Fix dtypes and Duplicate Rows” is free to read here on the web, and the Data Science Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Data Science Academy course, upgrade to CoddyKit PRO.
What will I learn in “Fix dtypes and Duplicate Rows”?
Coercing types and dropping repeats. You practise Data Science Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Data Science Academy?
No prior experience is required. Data Science Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Fix dtypes and Duplicate Rows” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Data Science Academy lesson?
Yes. Every Data Science Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Find the NaNs Hiding in Your Table
- Drop vs Fill: Choosing Wisely
- Impute With Mean, Median, or Mode
- Fix dtypes and Duplicate Rows