groupby后如何存储

How to store after groupby

使用 groupby 后如何使用该列? 比方说

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

会给出:

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

我想将收入最高的行业名称存储到某个变量中,并将其用于其他查询,所以这里是 "telecommunications"。 但是做 x[0] 会得到 330...

还请为这个问题推荐一个更好的措辞...不知道正确的术语

groupby(...).XXX()(其中 XXX 是某种支持方法,例如 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')

如果你想得到max/min值的索引,你可以使用idxmax/idxmin:

>>> x.idxmax()
'telecommunication'

>>> x.idxmin()
'crypto'

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

您也可以使用以下代码将其转换为另一个数据框

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