Skip to content
Advertisement

Pandas: Tidy up groupby aggregation

I really struggle with tidying up the table into a “normal” dataframe again after having aggregated something. I had a table like that (columns):

JavaScript

So I calculated average and std of the Result column over multiple runs using that command:

JavaScript

The output is a DataFrame like that:

JavaScript

It looks a bit like three levels.

df.columns outputs the following multiindex:

JavaScript

How do I flatten that again, removing “Result” and putting mean and std into the same “level” as the rest? There are so many commands like reset_index, drop_level and so on, but I did not find out yet how to fix that. It quite confuses me.

Edit: For reproducability, here is my entire code:

JavaScript

And the (shortened) csv file Results.csv:

JavaScript

Advertisement

Answer

Use reset_index() and then flatten the indexes:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement