DEV Community

bao han
bao han

Posted on

Catch repeated patient-visit keys before joining two research exports

A follow-up export can contain multiple visits per participant. Before joining it to another visit-level file, check whether each participant-and-visit pair occurs once. A join on the participant alone can match several records on each side.

The following rows are fictional. The duplicate could be a correction, a repeated export, or a real second visit recorded at the same nominal time. The check makes that case visible for review.

from collections import Counter

rows = [
    ("P01", "baseline", 12),
    ("P01", "month_1", 11),
    ("P01", "month_1", 10),
    ("P02", "baseline", 9),
]

counts = Counter((participant, visit) for participant, visit, _ in rows)
duplicates = sorted((key, count) for key, count in counts.items() if count > 1)
assert duplicates == [(("P01", "month_1"), 2)]
print(duplicates)
Enter fullscreen mode Exit fullscreen mode

The output is [(('P01', 'month_1'), 2)]. Check the matching source records and the collection protocol before deciding whether to keep both. If two observations at the same nominal visit are expected, define a key that also distinguishes those observations. Run the same check on each input, then verify the joined row count against what the study design permits.

Python's Counter documentation

Top comments (0)