Sometimes data has repeated rows that we don't need. Removing duplicates helps us clean data and get accurate results.
Removing duplicates (drop_duplicates) in Data Analysis Python
DataFrame.drop_duplicates(subset=None, keep='first', inplace=False)
subset lets you choose columns to check for duplicates. If None, all columns are checked.
keep decides which duplicate to keep: 'first' keeps the first, 'last' keeps the last, and False drops all duplicates.
df.drop_duplicates()
df.drop_duplicates(subset=['Name', 'Age'])
df.drop_duplicates(keep='last')df.drop_duplicates(inplace=True)This code creates a small table with repeated rows. Then it removes duplicates and shows the cleaned table.
import pandas as pd data = {'Name': ['Alice', 'Bob', 'Alice', 'David', 'Bob'], 'Age': [25, 30, 25, 40, 30], 'City': ['NY', 'LA', 'NY', 'Chicago', 'LA']} df = pd.DataFrame(data) print('Original DataFrame:') print(df) # Remove duplicate rows unique_df = df.drop_duplicates() print('\nDataFrame after removing duplicates:') print(unique_df)
By default, drop_duplicates() returns a new DataFrame and does not change the original.
Use inplace=True if you want to modify the original DataFrame directly.
Checking duplicates on specific columns helps when only some columns matter for uniqueness.
Use drop_duplicates() to remove repeated rows from data.
You can choose which columns to check and which duplicates to keep.
Removing duplicates helps make data clean and ready for analysis.
Practice
drop_duplicates() function do in a pandas DataFrame?Solution
Step 1: Understand the function purpose
drop_duplicates()is used to remove repeated rows in a DataFrame.Step 2: Default behavior
By default, it checks all columns to find duplicates and keeps the first occurrence.Final Answer:
Removes repeated rows based on all columns by default -> Option BQuick Check:
drop_duplicates() removes repeated rows [OK]
- Thinking it sorts data
- Confusing with fillna()
- Assuming it changes data types
df?Solution
Step 1: Identify correct method call
The method to remove duplicates isdrop_duplicates(), called with parentheses.Step 2: Check syntax correctness
Onlydf.drop_duplicates()is valid syntax; brackets or braces are incorrect.Final Answer:
df.drop_duplicates() -> Option CQuick Check:
Use parentheses to call methods [OK]
- Using square brackets instead of parentheses
- Using a non-existent method name
- Using curly braces instead of parentheses
df below, what will df.drop_duplicates(subset=['Name']) return?import pandas as pd
data = {'Name': ['Anna', 'Bob', 'Anna', 'Cody'], 'Age': [25, 30, 25, 22]}
df = pd.DataFrame(data)Solution
Step 1: Understand subset parameter
Usingsubset=['Name']means duplicates are checked only on the 'Name' column.Step 2: Identify duplicates by 'Name'
'Anna' appears twice; only the first occurrence is kept, others dropped.Final Answer:
[{'Name': 'Anna', 'Age': 25}, {'Name': 'Bob', 'Age': 30}, {'Name': 'Cody', 'Age': 22}] -> Option AQuick Check:
Duplicates dropped by 'Name' column [OK]
- Not using subset and dropping all duplicates
- Keeping all duplicates instead of first
- Confusing which rows are dropped
df?df.drop_duplicates(inplace=True)
After running it, duplicates still appear in
df. Why?Solution
Step 1: Check inplace parameter usage
inplace=Truemodifies the original DataFrame, so no reassignment needed.Step 2: Consider DataFrame content
If duplicates still appear, likely the DataFrame had no duplicates to remove.Final Answer:
The DataFrame has no duplicates to remove -> Option AQuick Check:
inplace=True modifies original; no duplicates means no change [OK]
- Assuming inplace=True does nothing
- Thinking drop_duplicates always changes data
- Not verifying if duplicates exist
df with columns ['ID', 'Name', 'Score']. Some rows have duplicate ID values but different Score. How do you keep only the row with the highest Score for each ID?Solution
Step 1: Sort DataFrame by Score descending
Sorting by 'Score' descending puts highest scores first for each ID.Step 2: Drop duplicates by ID keeping first
After sorting, dropping duplicates by 'ID' keeps the first (highest score) row per ID.Final Answer:
df.sort_values('Score', ascending=False).drop_duplicates(subset=['ID']) -> Option DQuick Check:
Sort then drop duplicates keeps highest score per ID [OK]
- Not sorting before dropping duplicates
- Dropping duplicates on wrong column
- Using keep='last' without sorting
Ready to prove it?
Pass the test and this pattern moves unknown
