BBampaa

Lessons › Python

Python lesson

Duplicates

One duplicate: Musa appears twice. Left alone, his order would be counted twice.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

A row copied twice gets counted twice. .duplicated() finds rows that repeat an earlier one:

print(df.duplicated().sum())

Output:

1

Key points

.duplicated()True for every row that repeats an earlier row.
.drop_duplicates()Removes the repeated rows.
df = df.drop_duplicates()Store it back in df, or the duplicates stay.

Put together: df.duplicated().sum() to count them, df = df.drop_duplicates() to remove them.

Practice

Try each one in your head first, then open the answer.

1. Build a line that removes the duplicate row from df.
df = df.drop_duplicates()
2. Build a line that shows how many duplicate rows there are.
df.duplicated().sum()
3. Show the duplicate row itself.
df[df.duplicated()]
4. Remove the duplicate row, then show how many rows are left.
df = df.drop_duplicates()
len(df)
5. Remove the duplicate row, then show how many duplicates are left.
df = df.drop_duplicates()
df.duplicated().sum()

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed