I have the following dataframe dfg (which is a result of previous aggregations).
F-1 F-2
dataset Model
G Baseline 0.971 0.967
Version2 0.971 0.967
H Baseline 0.780 0.762
Version2 0.800 0.777
S Baseline 0.401 0.320
Version2 0.453 0.365
T Baseline 0.881 0.825
Version2 0.989 0.985
What I want is to obtain the following organization of my dataframe:
Baseline Version2
dataset F-1 F-2 F-1 F-2
G 0.971 0.967 0.971 0.967
H 0.780 0.762 0.800 0.777
S 0.401 0.320 0.453 0.365
T 0.881 0.825 0.989 0.985
I tried several things, but what I thought were the best solutions always gave me errors. My most "logical" solution was:
- reset the index (to extract 'Model' into columns);
- create the multi-level column from tuples;
- changing the columns into multi-level columns.
like this:
dfg.reset_index(inplace=True, level=['Model']
new_cols = [('Baseline', 'F-1'), ('Baseline', 'F-2'), ('Version2', 'F-1'), ('Version2', 'F-2')]
multi_cols = pd.MultiIndex.from_tuples(new_cols, names=('Model', 'Measure'))
but I'm getting the following errors:
ValueError: Length mismatch: Expected axis has 3 elements, new values have 4 elements
I know this is rather raw, but I can't find any source that can explain how to build multi-level columns from existing dataframes.