Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: index of max value for each group

My Pandas DataFrame, df, looks like this:

parameter1   parameter2   value

1            1            0.1
             2            0.2

2            1            0.6
             2            0.3

value is the result of a groupby(['parameter1','parameter2']).mean() on another DataFrame. Now, I can find the maximum value of value for each value of parameter1 using

df.max(level='parameter1')

However, I need to find the corresponding value of parameter2 for this maximum value. It seems df.idxmax() does not support level=, so how can I do this instead?

like image 795
Thomas Arildsen Avatar asked Aug 19 '15 14:08

Thomas Arildsen


1 Answers

A nice way would be

df.unstack().idxmax(axis=1)

Unstacking the dataframe gives a dataframe with parameter_1 as the column index.

like image 173
JoeCondron Avatar answered Sep 21 '22 23:09

JoeCondron