I have a Pandas dataframe which looks as follows: Starting Table:
| Kode | Country | Procedure | male | male | female | female |
|---|---|---|---|---|---|---|
| Kode | Country | Procedure | two-year-old | three-year-old | two-year-old | three-year-old |
| 1a | US | proc_1 | 4 | 6 | 3 | 6 |
| 1a | UK | proc_1 | 2 | 3 | 5 | 1 |
| 1b | US | proc_2 | 15 | 3 | 5 | 2 |
| 1b | UK | proc_2 | 8 | 4 | 7 | 3 |
CSV:
Code;Country;Procedure;male;male;female;female Code;Country;Procedure;two-year-old;three-year-old;two-year-old;three-year-old 1a;US;proc_1;4;6;3;6 1a;UK;proc_1;2;3;5;1 1b;US;proc_2;15;3;5;2 1b;UK;proc_2;8;4;7;3 My python code:
df = pd.read_csv('test.csv', header=[0,1],delimiter= ';', engine='python', encoding= 'unicode_escape') In fact I have two row headers How to pivot pandas dataframe to get the following result?
Resulting Table:
| Code | Country | Procedure | Gender | Age | Frequency |
|---|---|---|---|---|---|
| 1a | US | proc_1 | male | two-year-old | 4 |
| 1a | US | proc_1 | male | three-year-old | 6 |
| 1a | US | proc_1 | female | two-year-old | 3 |
| 1a | US | proc_1 | female | three-year-old | 6 |
| 1a | UK | proc_1 | male | two-year-old | 2 |
| 1a | UK | proc_1 | male | three-year-old | 3 |
| 1a | UK | proc_1 | female | two-year-old | 5 |
| 1a | UK | proc_1 | female | three-year-old | 1 |
| 1b | ... |