Pivot Sensor Readings Into One Row Per Device
Problem
You are given one DataFrame `readings` with columns `device_id`, `gauge`, and `value`, where `gauge` is the name of a measurement station and `value` is the reading that device produced at that station. There is at most one reading per `(device_id, gauge)`.
Reshape the data into a wide format: output `device_id` as the first column, then one column for each distinct gauge name (sorted alphabetically), holding that device's `value` at that gauge, or `None` if the device has no reading there. For this dataset the gauges are `Alpha`, `Bravo`, `Delta`, `Echo`. Return one row per device.
Input data
Example rows — the live problem includes the full dataset.
| device_id | gauge | value |
|---|---|---|
| 1 | Alpha | 12 |
| 1 | Delta | 40 |
| 2 | Bravo | 7 |
| 2 | Echo | 55 |
| 3 | Alpha | 9 |
Expected output
Your answer should return 3 rows with the columns device_id, Alpha, Bravo, Delta, Echo.
Starter code (Pandas (Python))
import pandas as pd
def pivot_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