MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Handling Duplicates & Data Type Conversions0%
Study MaterialsCheatsheet
PandasProgress 0%
1.1What is Pandas & Core Architecture1.2Pandas Installation & Environment Setup1.3Creating Series & DataFrames from Scratch
4.1Detecting Missing Values (isna, notna, isnull)4.2Dropping & Filling Missing Values (dropna, fillna)4.3Handling Duplicates & Data Type Conversions
HomeTutorialsPandasHandling Duplicates & Data Type Conversions
Applying Custom Functions (apply, map, applymap)

Handling Duplicates & Data Type Conversions

Intermediate6 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

1. Detecting & Removing Duplicate Records

Duplicate rows often occur due to repeated form submissions, database re-syncs, or ETL joining errors.

1import pandas as pd
2 
3df = pd.DataFrame({
4 'Transaction_ID': [101, 102, 103, 102, 104],
5 'User': ['Amit', 'Priya', 'Rohan', 'Priya', 'Sneha'],
6 'Amount': [1200, 4500, 3100, 4500, 8900]
7})
8 
9# 1. Identify duplicates
10print(df.duplicated()) # Returns boolean Series
11 
12# 2. Check for duplicate Transaction IDs only
13print(df.duplicated(subset=['Transaction_ID']))
14 
15# 3. Drop duplicate rows (keep the first occurrence)
16df_unique = df.drop_duplicates(subset=['Transaction_ID'], keep='first')
17print(df_unique)

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:

1# Converting data types
2df['Transaction_ID'] = df['Transaction_ID'].astype(str)
3df['Amount'] = df['Amount'].astype(float)

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:

1raw_prices = pd.Series(['150.50', '200.00', 'corrupted_val', '310.25'])
2 
3# errors='coerce' turns 'corrupted_val' into NaN instead of crashing
4clean_prices = pd.to_numeric(raw_prices, errors='coerce')
5print(clean_prices)

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:

1df['User'] = df['User'].astype('category')

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
Which method drops duplicate records from a DataFrame?
Question 2
What happens when calling df.drop_duplicates(keep=False)?
Question 3
What does pd.to_numeric(series, errors='coerce') do when encountering unparseable text?
Question 4
Which datatype should be used for text columns with low cardinality (few unique strings repeated millions of times) to save memory?
Question 5
How do you convert an integer column 'Age' into a 32-bit float in Pandas?

Next Lesson

Applying Custom Functions (apply, map, applymap)

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext 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.

12-Month Data Analysis Mastery Program

Master Full-Stack Data Analytics (Excel, SQL, Python, Power BI & Tableau)

Combine 15 complete courses, 150 chapters, 541 topics, live SQL/Power BI/Tableau dashboards, and Scikit-Learn capstones at MSK Institute Shikohabad or Live Online.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext