Skip to content Skip to sidebar Skip to footer

Python Pandas Groupby Forloop & Idxmax

I have a DataFrame that must be grouped on three levels, and would then have the highest value returned. Each day there is a return for each unique value, and I would like to find

Solution 1:

I think, if I understand you correctly, you could collect the index values in a Series using groupby and idxmax(), and then select those rows from df using loc:

idx =  data.groupby(['Company','Product','Industry'])['ROI'].idxmax()
data.loc[idx]

another option is to use reindex:

data.reindex(idx)

On a (different) dataframe I happened to have handy, it appears reindex might be the faster option:

In[39]: %timeitdf.reindex(idx)
10000loops, bestof3: 121usperloopIn[40]: %timeitdf.loc[idx]10000loops, bestof3: 147usperloop

Post a Comment for "Python Pandas Groupby Forloop & Idxmax"