Unpivot Sensor Readings Into Tall Format
Problem
An IoT platform stores readings in a wide format. The DataFrame `readings` (loaded from `readings.csv`) has one row per device with columns `device_id`, `Alpha`, `Bravo`, `Delta`, `Echo`, where each non-id column is the reading that device produced at the gauge of that name, or NaN if there is no reading there.
Reshape the data into a tall format with exactly three columns: `device_id`, `gauge`, and `value`. Produce one row per (device, gauge) where a reading actually exists; skip any combination whose value is missing.
Return the result in any order.
Input data
Example rows — the live problem includes the full dataset.
| device_id | Alpha | Bravo | Delta | Echo |
|---|---|---|---|---|
| 1 | 12 | 40 | ||
| 2 | 7 | 55 | ||
| 3 | 9 | 8 | 3 | 2 |
Expected output
Your answer should return 8 rows with the columns device_id, gauge, value.
Starter code (Pandas (Python))
import pandas as pd
def unpivot_sensor_readings(readings) -> pd.DataFrame:
# Your code here
return readingsSolve 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