Home > front end >  How to store after groupby
How to store after groupby

Time:12-22

How am I able to use the column after using groupby? Let's say

x = df_new.groupby('industry')['income'].mean().sort_values(ascending = False)

would give:

"industry"        
telecommunications        330
crypto.                   100
gas                       100
.
.
.

I would like to store the top most income's industry name to some variable and use it for other queries, so here it would be "telecommunications". But doing x[0] would give 330...

also pls recommend a better wording for this question... don't know the right terminologies

CodePudding user response:

groupby(...).XXX() (where XXX is some support method, e.g. mean, sort_values, etc.) typical return Series objects, where the index contains the values that were grouped by. So you can use x.index:

>>> x.index
Index(['telecommunication', 'crypto', 'gas'], dtype='object', name='industry')

If you want to get index for the max/min value, you can use idxmax/idxmin:

>>> x.idxmax()
'telecommunication'

>>> x.idxmin()
'crypto'

>>> x.index[2]
'gas'
  • Related