如何在使用聚合函数时查看列

How to view the column while using aggregate function

我有下面的代码,它将聚合客户状态列并显示 payment_value 列的最高平均值。 data.groupby('customer_state').agg({'payment_value':'mean'}).max()

下面是我得到的输出:

payment_value    258.00294

我希望我的输出与 customer_state 列名称一起显示。如下所示,其中 PB 是 customer_state:

PB  258.002940

因为参数customer_state没有转到列,需要先重置索引,重命名列payment_value,这样操作起来会更方便。

data.groupby(['customer_state']).agg({'payment_value': ['mean']}).reset_index()
data.columns = ['customer_state', 'payment_value']
data =  data.sort_values(['payment_value'], ascending = False)