Pandasdrill

Level 2: Cleaning Exercise 23 of 63 Pro

Fill gaps with interpolate

A pandas exercise from Pandasdrill. Solve it with a Python editor in your browser running real pandas, and it is checked the moment you submit.

The task

The hourly `temp` readings have gaps, but the first and last readings are present. Fill each missing value by linear interpolation between the readings before and after it, and return the `temp` column as a Series.

What this exercise teaches

Linear interpolation spaces the missing values evenly between the known neighbors: between 23.5 and 26.5 with two gaps you get 24.5 and 25.5. ffill would repeat the last reading, and filling with the mean ignores where in the sequence the gap sits.

A hint if you get stuck

Series.interpolate fills NaN by drawing a straight line between the nearest known values.

Try it now, free, no account needed

The first 15 tasks on Pandasdrill are free. Open it on the practice page, solve it, and get instant feedback with a hint. A free account adds the worked solution; Pro adds timed mode and the certificate.

Open this exercise

More exercises in Cleaning