AnalystPath

Unfold Shift Readings

PandasEasyJunior level~10 min

Problem

A factory keeps machine output in a wide DataFrame `shiftlog` with columns `machine_id`, `morning`, `afternoon`, and `night`. Each shift column holds that machine's reading for the shift, or a missing value (NaN) when the machine was idle.

Reshape it into a long DataFrame with one row per `(machine_id, shift, reading)`. The `shift` column should contain the literal strings `'morning'`, `'afternoon'`, or `'night'`. Do **not** emit a row when the reading for a shift is missing.

Return the columns `machine_id`, `shift`, `reading` in any order.

Input data

Example rows — the live problem includes the full dataset.

shiftlog
machine_idmorningafternoonnight
095100105
17080

Expected output

Your answer should return 5 rows with the columns machine_id, shift, reading.

Starter code (Pandas (Python))

import pandas as pd

def unfold_shift_readings(shiftlog) -> pd.DataFrame:
    # Your code here
    return shiftlog

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