BBampaa

Lessons › Python

Python lesson

Gaps and duplicates

One revenue is missing (Grace's, shown as NaN) and one row is a duplicate (Musa appears twice). Left alone, the duplicate would count Musa's order twice.

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

Example

Two problems hide in most real files: empty cells and repeated rows. pandas finds both:

print(df["Revenue"].isna().sum())
print(df.duplicated().sum())

Output:

1
1

Key points

.isna().sum()How many values are missing in a column.
.fillna(0)Fill the missing values with something, like 0.
.drop_duplicates()Remove repeated rows.

Put together: df = df.drop_duplicates() then df["Revenue"] = df["Revenue"].fillna(0).

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 fills the missing revenue with 0.
df["Revenue"] = df["Revenue"].fillna(0)
3. Show how many revenue values are missing.
df["Revenue"].isna().sum()
4. Fill the missing revenue with 0.
df["Revenue"] = df["Revenue"].fillna(0)
5. Remove the duplicate row, then show how many rows are left.
df = df.drop_duplicates()
len(df)

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