You can manually create your Visits-Test-Sample dataframe, then merge
with Patients dataframe:
pd.MultiIndex.from_product([['V2','V3'],['Test 1', 'Test 2'],['A', 'B']], names=['Visit', 'Test', 'Sample'])
.union(pd.MultiIndex.from_product([['V1'],['Test 1'],['A','B']], names=['Visit', 'Test', 'Sample']))
.to_frame().reset_index(drop=True)
.merge(df, on='Visit')
.sort_values('Patient')
Output:
Visit Test Sample Patient
0 V1 Test 1 A 1
2 V1 Test 1 B 1
4 V2 Test 1 A 1
6 V2 Test 1 B 1
8 V2 Test 2 A 1
10 V2 Test 2 B 1
12 V3 Test 1 A 1
13 V3 Test 1 B 1
14 V3 Test 2 A 1
15 V3 Test 2 B 1
1 V1 Test 1 A 2
3 V1 Test 1 B 2
5 V2 Test 1 A 2
7 V2 Test 1 B 2
9 V2 Test 2 A 2
11 V2 Test 2 B 2
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…