How can I identify duplicate rows in a dataset without dropping them, and display the count of duplicates for each employee ID (e.g., employee ID 1, John, is duplicated)?
💡 Model Answer
Using pandas you can add a column that shows how many times each employee_id appears, without removing any rows. The key function is groupby with transform:
# Assume df is your DataFrame
df['duplicate_count'] = df.groupby('employee_id')['employee_id'].transform('count')
# Now df contains a new column duplicate_count. Rows where duplicate_count > 1 are duplicates.
# To view only duplicates:
duplicates = df[df['duplicate_count'] > 1]
print(duplicates[['employee_id', 'name', 'duplicate_count']])
This keeps all original data intact, simply annotates each row with how many times that employee_id occurs. Complexity is O(n) for the transform and O(n) for filtering, so overall linear in the number of rows.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.
Get Assisting AI — Starts at ₹500