Retaining full index in Pandas with Groupby

I have this code:

suicide_data.groupby(['State','Type'])['Total'].sum()

image

Now I want that for each state, which type is maximum.
When I write this code-

suicide_data.groupby(['State','Type']).sum().groupby('State')['Total'].max()

image

It gives the state name and maxium value but not the type column. Can anyone tell what changes should I make to the code to get type column as well?

s = suicide_data.groupby(['State', 'Type'])['Total'].sum()
s[s.groupby('State').transform(lambda x: x == x.max())]

1 Like

Thanks a lot @sanjayk