Understand the idea
fillna replaces missing values only. The fill rule is an assumption, so choose it explicitly and retain evidence of the original gaps.
A small example
[2, missing, 6] filled with its observed median becomes [2, 4, 6]; the 4 is an estimate.
Follow the code
Apply the idea to the supplied table. Read from top to bottom; the final line displays the result.
df["tip"] = df["tip"].fillna(df["tip"].median())
dfWhat each part does
median()- middle observed value
fillna(value)- replace gaps only
Other choices for later exercises
mode().iloc[0]- first most-common value; ties need a decision
Your inputs
The editable setup on the right creates df. Run executes the setup and your work from top to bottom.
| order | drink | size | price | tip | date |
|---|---|---|---|---|---|
| 101 | " latte " | LARGE | 6.20 | 1 | 2026-06-01 |
| 102 | TEA | small | 3.10 | 0.5 | 2026-06-02 |
| 103 | " mocha " | LARGE | oops | None | not a date |
| 104 | Latte | Small | None | 0.8 | 2026-06-04 |
| 104 | Latte | Small | None | 0.8 | 2026-06-04 |
| 105 | "tea " | SMALL | 4.20 | 0.6 | 2026-06-05 |
| 106 | ESPRESSO | small | 2.50 | 0.2 | 2026-06-06 |
| 107 | " mocha" | large | 6.80 | 1.5 | 2026-06-07 |
Your task · Follow
- Using df, fill missing tip with the observed median tip, keeping all rows.
- This is an exercise assumption, not proof of the true tip.
- Keep the changes in df and display it.
- Use: fillna().
Hint
Calculate the observed median before filling; do not replace known values.
Reveal solution
One way to do it. Keep any supplied setup in the editor and use this in the Your work section.
df["tip"] = df["tip"].fillna(df["tip"].median())
df