Interpolating Missing Temperatures
Start Timer
0:00:00
A climate research organization has a time-series dataframe with daily temperature readings for different cities. This dataframe has three columns: date
, city
, and temperature
.
Due to data recording issues, the temperature reading for some days might be missing. The organization requires the temperature readings for each day, so they ask you to interpolate the missing data.
Write a Python function using Pandas that uses a linear interpolation to estimate the missing data and fill out the dataframe.
Notes:
- When estimating the missing values for a city, the interpolation should only consider data from the same city.
- Temperature recording issues are rare, so you can assume that there is no missing data two days in a row.
- You can also assume that both the first and the last dates in your dataframe hold valid temperature data.
Example:
Input:
date | city | temperature |
---|---|---|
2023-01-01 | London | 10 |
2023-01-02 | London | NaN |
2023-01-03 | London | 12 |
2023-01-04 | London | NaN |
2023-01-05 | London | 14 |
2023-01-01 | Berlin | -2 |
2023-01-02 | Berlin | -1 |
2023-01-03 | Berlin | NaN |
2023-01-04 | Berlin | 1 |
2023-01-05 | Berlin | 2 |
Output:
date | city | temperature |
---|---|---|
2023-01-01 | London | 10 |
2023-01-02 | London | 11 |
2023-01-03 | London | 12 |
2023-01-04 | London | 13 |
2023-01-05 | London | 14 |
2023-01-01 | Berlin | -2 |
2023-01-02 | Berlin | -1 |
2023-01-03 | Berlin | 0 |
2023-01-04 | Berlin | 1 |
2023-01-05 | Berlin | 2 |
Recommended questions for you
Personalized based on your user activity, skill level, and preferences.
.
.
.
.
.
.
.
.
.
Comments