[英]pandas group by, aggregate using multiple agg functions on input columns
I am looking to do some aggregation on a pandas groupby dataframe, where I need to apply several different custom functions on multiple columns. 我想在一个pandas groupby数据帧上做一些聚合,我需要在多个列上应用几个不同的自定义函数。 This operation is very easy and customary in R (using
data.table
or dplyr
), but I am surprised I'm finding it so difficult in pandas: 这个操作在R中非常容易和习惯(使用
data.table
或dplyr
),但我很惊讶我在熊猫中发现它很难:
import pandas as pd
data = pd.DataFrame({'A':[1,2,3,4,5,6],'B':[2,4,6,8,10,12],'C':[1,1,1,2,2,2]})
#These work
data.groupby('C').apply(lambda x: x.A.mean() - x.B.mean())
data.groupby('C').agg(['mean','std'])
#but this doesn't
data.groupby('C').agg([lambda x: x.A.mean() - x.B.mean(),
lambda x: len(x.A)])
I want to calculate a statistic but also the sample size in each group, which seems like it should be a one or two line solution, but I also sometimes need to apply multiple functions on multiple columns of the grouped data frame. 我想计算一个统计量,但也计算每个组中的样本大小,这似乎应该是一个或两个行解决方案,但我有时也需要在分组数据框的多个列上应用多个函数。
We can write a function that does the custom functions on multiple columns and returns the result as a data frame. 我们可以编写一个函数,在多个列上执行自定义函数,并将结果作为数据框返回。
>>> def meandiff_length(data):
data['mean_diff'] = data.A.mean() - data.B.mean()
data['a_length'] = len(data.A)
return data
We can group the data and apply the custom function to the groups separately. 我们可以对数据进行分组,并将自定义函数分别应用于组。
>>> data.groupby('C').apply(meandiff_length)
A B C mean_diff a_length
0 1 2 1 -2.0 3
1 2 4 1 -2.0 3
2 3 6 1 -2.0 3
3 4 8 2 -5.0 3
4 5 10 2 -5.0 3
5 6 12 2 -5.0 3
This specific custom function returns the same value in every row, so it may be of your interest to use drop_duplicates
. 此特定自定义函数在每一行中返回相同的值,因此使用
drop_duplicates
可能是您感兴趣的。 However, this is a general solution that will also work when our custom function becomes more complex. 但是,这是一个通用的解决方案,当我们的自定义功能变得更复杂时也可以使用。
If you need a one-liner, you can do this: 如果您需要单行,您可以这样做:
#use apply instead of agg to create multiple columns
data.groupby('C').apply(lambda x: pd.Series([x.A.mean() - x.B.mean(), len(x.A)])).rename(columns={0:'diff',1:'a_len'})
Out[2346]:
diff a_len
C
1 -2.0 3.0
2 -5.0 3.0
Another solution without using rename. 不使用重命名的另一种解决方案
data.groupby('C').apply(lambda x: pd.DataFrame([[x.A.mean() - x.B.mean(), len(x.A)]],columns=['diff','a_len']))
Out[24]:
diff a_len
C
1 0 -2.0 3
2 0 -5.0 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.