BBampaa

Lessons › Python

Python lesson

Missing values

One revenue is missing: Grace's. Left alone, it's skipped by sums and averages without any warning.

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

Example

Empty cells hide in most real files. pandas shows them as NaN, and .isna() finds them:

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

Output:

1

Key points

.isna()True for every missing value.
.isna().sum()How many are missing: each True counts as 1.
.fillna(0)Fill the missing values with something, like 0.

Put together: df["Revenue"].isna().sum() to count them, df["Revenue"] = df["Revenue"].fillna(0) to fill them.

Practice

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

1. Build a line that shows how many revenue values are missing.
df["Revenue"].isna().sum()
2. Build a line that fills the missing revenue with 0.
df["Revenue"] = df["Revenue"].fillna(0)
3. Show the rows where revenue is missing.
df[df["Revenue"].isna()]
4. Fill the missing revenue with the average revenue.
df["Revenue"] = df["Revenue"].fillna(df["Revenue"].mean())
5. Fill the missing revenue with 0, then show how many are still missing.
df["Revenue"] = df["Revenue"].fillna(0)
df["Revenue"].isna().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