AnalystPath

Carry Forward the Thermostat Setting

PandasMediumMid level~10 min

Problem

You are given a DataFrame `thermostatlog` (loaded from `thermostatlog.csv`) with columns `reading_seq` (the order of the reading) and `target_temp`. Some `target_temp` values are missing (null) because the thermostat only logs a value when the setting changes.

Fill each missing `target_temp` with the most recent non-null value from an earlier reading (ordered by `reading_seq`). Return the columns `reading_seq` and `target_temp`, ordered by `reading_seq`.

Input data

Example rows — the live problem includes the full dataset.

thermostatlog
reading_seqtarget_temp
1Warm 72F
2
3
4Eco 68F
5Cool 65F

Expected output

Your answer should return 6 rows with the columns reading_seq, target_temp.

Starter code (Pandas (Python))

import pandas as pd

def carry_forward_the_thermostat_setting(thermostatlog) -> pd.DataFrame:
    # Your code here
    return thermostatlog

Solve this Pandas question free

Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related Pandas questions