An approach using melt, groupby and unstack:
Data
Original
In []: df
Out[]:
Patient Test panel gene alteration
0 1 A 54 APC E1345*
1 1 B 54 TP53 Y205H
2 1 C 54 APC V2278V
3 2 A 54 KRAS G12D
4 2 B 54 PTEN L25L
5 3 A 54 KRAS G13D
6 3 C 54 TP53 C141W
7 3 C 54 APC R876*
8 3 A 54 ERBB2 L663P
Tidy data
pd.DataFrame.melt allows to tidy this table:
In []: tidy = df.melt(id_vars=['Patient', 'Test'], value_vars=['panel', 'gene', 'alteration'])
In []: tidy
Out[]:
Patient Test variable value
0 1 A panel 54
1 1 B panel 54
2 1 C panel 54
3 2 A panel 54
4 2 B panel 54
5 3 A panel 54
6 3 C panel 54
7 3 C panel 54
8 3 A panel 54
9 1 A gene APC