Research Workflow

Reshape Repeated Measurements Without Aggregating Them Away

Define one observation

Long data commonly store one participant-visit-measurement per row; wide data may store several visits in separate columns. Neither format is inherently correct for every analysis. Before reshaping, write the key that identifies one measurement, including specimen, side or replicate where relevant.

The pandas reshaping documentation distinguishes pivoting from pivot-table aggregation. That distinction matters: duplicate keys should trigger a scientific decision, not disappear because a convenient function averages them by default.

State the expected transformation

List identifier columns, measurement columns and the new visit or variable labels. Preserve units and missing-value meaning. If a participant has no record for a visit, decide whether the wide result should contain an explicit missing cell and whether that absence differs from a recorded but unavailable measurement.

Before pivoting, count rows per proposed key. More than one row may represent duplicate entry, repeated assays or legitimate multiple encounters. Resolve the reason before selecting a value or calculating an average. Record the aggregation rule if aggregation is scientifically intended.

Check a hypothetical table

Suppose three participants each have baseline and week-four measurements. Complete long data contain six rows and reshape to three participant rows with two measurement columns. If one participant lacks week four, the long table may contain five rows; the wide table can still contain three participants with one missing measurement cell.

Now add a second week-four row for participant P02. A pivot requiring unique participant-visit keys should flag the ambiguity. A pivot table using the mean might produce a seemingly tidy output, but it has silently made a repeat-measurement rule. If the two values are 8 and 12, the mean 10 is a derived quantity, not either original observation.

Validate the round trip

Check participant count, nonmissing measurement count, key uniqueness and measurement values before and after reshaping. Reshape a small synthetic dataset back to its original representation and compare sorted keys and values. A row-count match alone does not detect swapped visit labels.

Keep a mapping from original column names to visit and measurement definitions. For mixed outcomes, do not place incompatible units in one unnamed value column. The final data layout should make the intended observation unit easier to analyze while preserving every selection, aggregation and missing-cell decision needed to understand where its values came from.

Source verification: 6 November 2026. Examples are hypothetical.

Related reading

Reading path: Prepare data. Guide 23 of 50. Choose any guide to suit your project.

Research Workflow

Balance assay batches before processing samples

Keep biological comparisons from becoming inseparable from plates, runs or processing dates.

Research Workflow

Audit a case-cohort sample before modelling

Preserve subcohort membership, cohort sizes and sampling strata so the analysis reflects the design.

Browse this reading pathTools for this stageGuides and downloads for this stage