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 exerciseMore exercises in Cleaning
- Fill missing values in one columnPro
- Drop rows missing a key fieldPro
- Convert text to numbersPro
- Tidy up text with strip and lowerPro
- Search text with str.containsPro
- Pull numbers out of codesPro
- Remove duplicate recordsPro
- Rename columns and replace a placeholderPro
- Dates with to_datetime and dtPro
- Clean messy money textPro
- Split a column into twoPro
- Parse dates in day/month/year orderPro