I have a pandas dataframe with a multi-index where several groupby aggregations have already been computed. I'm trying to pivot the final grouped column into headings, without any aggregation. I've managed to accomplish it with aggregation, but can't seem to figure out how to simply transpose it so the 2nd last column becomes headers and the last column becomes the values under each header.
A similar example:
df = pd.DataFrame.from_dict(
{'Classifier':['SVM']*6 + ['CNN']*6,
'Preprocess': (['None']*3 + ['PCA']*3)*2,
'Group': ([1]*2 + [2]*2)*3,
'Fold': ([1] + [2])*6,
'Accuracy': np.random.rand((12))
})
df = df.groupby(['Classifier', 'Preprocess', 'Group']).agg({'Accuracy': 'mean'})
print df
Accuracy
Classifier Preprocess Group
CNN None 1 0.912794
2 0.514453
PCA 1 0.694035
2 0.740769
SVM None 1 0.235105
2 0.817883
PCA 1 0.567313
2 0.657962
I'm trying to get it to look something like:
Accuracy - Group
Classifier Preprocess 1 2
CNN None 0.912794 0.514453
PCA 0.694035 0.740769
SVM None 0.235105 0.817883
PCA 0.567313 0.657962
I should note that the 'Accuracy' column actually ends up being a string because of extra calculations. Any ideas? I feel like I've tried every combination of weird pivot/stack/unstack/re-index, but the closest I can get is a very short table where everything is aggregated down oddly when I want to actually maintain the final column numbers (no aggregation).