在聚合操作中重命名 Lambda 函数返回的值

问题描述

我希望针对数据集中的每组发布者显示各种百分位数的值。我正在尝试以下操作:

vg.groupby(['Publisher']).agg({'Global_Sales':['mean','min','max','median',lambda x: x.quantile(0.5)]})

数据集的几行是:

   Rank                       Name Platform    Year         Genre Publisher  \
0     1                 Wii Sports      Wii  2006.0        Sports  Nintendo   
1     2          Super Mario bros.      nes  1985.0      Platform  Nintendo   
2     3             Mario Kart Wii      Wii  2008.0        Racing  Nintendo   
3     4          Wii Sports Resort      Wii  2009.0        Sports  Nintendo   
4     5   Pokemon Red/Pokemon Blue       GB  1996.0  Role-Playing  Nintendo   
5     6                     Tetris       GB  1989.0        Puzzle  Nintendo   
6     7      New Super Mario bros.       DS  2006.0      Platform  Nintendo   
7     8                   Wii Play      Wii  2006.0          Misc  Nintendo   
8     9  New Super Mario bros. Wii      Wii  2009.0      Platform  Nintendo   
9    10                  Duck Hunt      nes  1984.0       Shooter  Nintendo   

   NA_Sales  EUR_Sales  JAP_Sales  IND_Sales  Global_Sales  
0     41.49      29.02       3.77       8.46         82.74  
1     29.08       3.58       6.81       0.77         40.24  
2     15.85      12.88       3.79       3.31         35.82  
3     15.75      11.01       3.28       2.96         33.00  
4     11.27       8.89      10.22       1.00         31.37  
5     23.20       2.26       4.22       0.58         30.26  
6     11.38       9.23       6.50       2.90         30.01  
7     14.03       9.20       2.93       2.85         29.02  
8     14.59       7.06       4.70       2.26         28.62  
9     26.93       0.63       0.28       0.47         28.31   

现在我想为返回的 对象命名。我无法这样做。请指导,因为我是 Python 新手并试图建立我的基础。

解决方法

只是链接rename()方法:

vg.groupby(['Publisher']).agg({'Global_Sales':['mean','min','max','median',lambda x: x.quantile(0.5)]}).rename(columns={'<lambda_0>':'quantile'},level=1)

droplevel()rename()

vg.groupby(['Publisher']).agg({'Global_Sales':['mean',lambda x: x.quantile(0.5)]}).droplevel(0,1).rename(columns={'<lambda_0>':'quantile'})
,

您还可以从 doc of .agg() 中直接指定 agg() 函数中的列名作为关键字参数:

>>> vg.groupby(['Publisher']).agg(
...     min=('Global_Sales','min'),...     foo=('Global_Sales',lambda x: x.quantile(0.5)),... )
              min     foo
Publisher                
Nintendo   36.939  30.815

如您所见,这些关键字参数的参数是(列,聚合函数)元组。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...