Work with a long table, one row per kind of measurement¶
Draft: not yet validated
The code on this page was run inside MESA JupyterLab on 2026-11-13 and produced the output shown. Nobody has yet followed the steps there by hand, so a menu or a screen may differ. Tell us what you find.
Many archives store data long: one row per time and per kind of measurement, with
the number in a single value column. It is a tidy way to store data and an easy way
to average rainfall with temperature by accident.
You will: have the hourly observations summarised by measurement and reshaped to one column for each.
Time: 5 to 10 minutes. You need: the notebook from Load a file from your Data Store into a notebook, open in MESA JupyterLab, with its first cell run.
Steps¶
-
Check that
observations.csvis in yourmesa-practicefolder. If it is not, run the download cell on the practice data page again. Then run:obs = pd.read_csv("observations.csv") obs.shapeYou should see:
(336, 4) -
Look at the first rows:
obs.head()You should see: each timestamp twice, once for each kind of measurement.
gauge_id timestamp_utc measurement value 0 G03 2026-07-10T06:00:00Z rain_mm 0.0 1 G03 2026-07-10T06:00:00Z air_temp_c 13.8 2 G03 2026-07-10T07:00:00Z rain_mm 0.0 3 G03 2026-07-10T07:00:00Z air_temp_c 12.3 4 G03 2026-07-10T08:00:00Z rain_mm 0.0 -
Count the rows for each kind:
obs["measurement"].value_counts()You should see:
measurement rain_mm 168 air_temp_c 168 Name: count, dtype: int64 -
Summarise each kind on its own:
obs.groupby("measurement")["value"].agg(["min", "max", "mean"]).round(2)You should see: two rows with nothing in common.
obs["value"].mean()would have returned 9.27, a number that is neither millimetres nor degrees.min max mean measurement air_temp_c 10.2 25.4 18.03 rain_mm 0.0 15.8 0.51 -
Reshape to one column per measurement:
wide = obs.pivot(index="timestamp_utc", columns="measurement", values="value") wide.head(3)You should see:
measurement air_temp_c rain_mm timestamp_utc 2026-07-10T06:00:00Z 13.8 0.0 2026-07-10T07:00:00Z 12.3 0.0 2026-07-10T08:00:00Z 10.7 0.0 -
Check the reshape lost nothing:
wide.shapeYou should see:
(168, 2).
Check it worked¶
The counts reconcile: 168 hours × 2 measurements is 336, the number of rows you
started with, and wide has no empty cells (wide.isna().sum().sum() is 0).
If it didn't¶
ValueError: Index contains duplicate entries, cannot reshape. Some timestamp has
two rows for the same measurement, so pandas cannot tell which value belongs in the
cell. Find them with
obs[obs.duplicated(["timestamp_utc", "measurement"], keep=False)] and settle which
is right before you reshape.
Next¶
- Turn UTC timestamps into local days
- Reference: Reshaping and pivot tables in the pandas user guide.
Not yet validated by a person in MESA JupyterLab. Drafted with AI assistance on 2026-11-13; the code was run there on 2026-11-13 (pandas 3.1.1, DuckDB 2.0.0).
Machine-readable versions of this page: Markdown twin · raw source on GitHub · llms.txt · llms-full.txt (whole site). See For AI agents.