This line:
gf = df.groupby(['weakness','type']).size()
gives the following output:
weakness type
fighting normal 6
fire bug 1
flying 1
grass 11
flying fighting 3
grass rock 1
water 1
ground electric 1
poison fairy 3
rock fire 1
water fire 9
ground 4
rock 4
dtype: int64
But how to get only the max 'type' of a certain 'weakness' as below. For counts having the same value, alphabetical order will be considered.
weakness type count
fighting normal 6
fire grass 11
flying fighting 3
grass rock 1 ......etc
What I tried
First I tried to convert the groupobject to a dataframe by:
gdf = gf.groupby(['weakness','type']).size().reset_index(name='count')
Then I tried:
gdf.groupby(["weakness","type"]).count().sort_values("count").groupby(level=0).tail(1)
which also didn't give the correct output. Any help will be appreciated.