A CSV can contain six measurements from three specimens. If each specimen has two instrument readings, your descriptive table should show both counts. For comparisons across specimens, the unit you count depends on how the intervention or sampling was assigned. For this example, each specimen is a separate unit and the repeated reads only check the instrument.

Test it on six readings

This synthetic calibration exercise has one missing reading. The blank remains missing. For an exploratory summary, B01 keeps its one available reading, and the output flags that it has fewer reads than planned.

from collections import defaultdict from csv import DictReader from io import StringIO from math import isfinite from statistics import fmean csv_text = """specimen,read,value A01,1,8 A01,2,10 B01,1,12 B01,2, C01,1,14 C01,2,16 """ groups = defaultdict(list) read_ids = set() for row in DictReader(StringIO(csv_text)): specimen = row["specimen"] key = (specimen, row["read"]) if key in read_ids: raise ValueError(f"Duplicate reading: {key}") read_ids.add(key) reads = groups[specimen] if row["value"].strip(): value = float(row["value"]) if not isfinite(value): raise ValueError(f"Non-finite measurement: {key}") reads.append(value) for specimen, reads in sorted(groups.items()): average = f"{fmean(reads):.1f}" if reads else "NA" row_count = sum(row_specimen == specimen for row_specimen, _ in read_ids) print(specimen, len(reads), row_count, average) 

The columns are specimen, usable reads, rows, and specimen mean:

A01 2 2 9.0 B01 1 2 12.0 C01 2 2 15.0 

The export has six rows and five usable readings from three specimens. Keep the unmodified export and this specimen summary. Check whether any specimen failed the minimum number of readings set in your protocol before using the summary in a comparison. If several specimens came from the same independently assigned unit, group at that level for the relevant inference. If repeat variability or nested effects matter, retain the repeat-level measurements for a model that represents that structure.

RigorMed is an AI research workspace for keeping the source table, analysis steps, and reviewable figures together. Open RigorMed.

References: Lazic et al., experimental units and sample size and Python statistics.fmean.

AI-generated text and code. The example uses synthetic data and was verified locally.