将Size()函数与聚合参数-Pandas一起应用于groupby

将Size()函数与聚合参数-Pandas一起应用于groupby,pandas,numpy,dataframe,automation,pandas-groupby,Pandas,Numpy,Dataframe,Automation,Pandas Groupby,我想获得使用groupby函数和agg参数时的实例数 Name Country X_Id Value Rahul 1 2 100 Rahul 1 2 50 Matthew 2 3 100 Matthew 1 1 25 Name Country X_Id Value Instances Rahul 1 2

我想获得使用groupby函数和agg参数时的实例数

Name    Country  X_Id   Value
Rahul   1          2      100
Rahul   1          2       50
Matthew 2          3      100
Matthew 1          1       25


Name    Country  X_Id    Value  Instances  
Rahul   1          2       50     2
Matthew 2          3      100     1
Matthew 1          1       25     1
我使用了
df.groupby(['SiteId','SubUnitId','CatId']).agg('mean').size()
,但它不适用于命名聚合:

df.groupby(['Name', 'Country', 'X_Id']).agg(Value = ('Value', 'mean'),
                                            Instances = ('Value', 'size')) 

输入数据正确吗?未将
2
3
交换,而代码函数对一列运行正常。。如果我必须对两列应用agg funct呢@jezrael@Harishreddy-然后添加新值,如
Instances=('Value','size')
表示
Instances
是新列名称,
Value
是处理列,
size
是聚合函数。因此,如果需要
col3
col4
使用
sum
使用
col4=('col3','sum')