在python中汇总 pandas 数据时,如何计算每组尾巴的和 [英] how to calculate sum|mean|median for tail of each group when pandas data aggregated in python
问题描述
我有如下数据,它是熊猫数据帧格式.
i am having data like following.which is in pandas data frame format.
A B C D E F G
1 1 2 3 1 4 2
1 1 2 4 5 6 7
1 1 2 3 2 3 2
1 1 2 4 5 6 7
2 1 2 3 2 3 4
2 1 2 3 4 3 3
2 1 2 4 5 6 7
此处agg_lvl = ['A','B','C']
here agg_lvl=['A','B','C']
当数据聚合到agg_lvl时,我想通过在每个组中使用tail(2)记录来计算G变量的均值| median | sum.
I want to calculate mean|median|sum for G variable by using tail(2) records in each group when data aggregated to agg_lvl.
我的预期输出是这样的:
And my expected output is like this:
期望的平均值输出:
A B C G
1 1 2 4.5
2 1 2 5
中位数和总和的输出也将相同,但是要代替平均值,我们必须考虑中位数和总和.
the output will be same for median and sum also,but in place of mean we have to consider median and sum values.
为此,我尝试了以下代码,但未获得预期的输出.
for that i tried the following code but i didn't get the expected output.
df.groupby(agg_lvl,as_index=False).tail(2).agg({'G':'mean'})
谁能帮我解决这个问题.
can anyone help me tackle this issue.
谢谢.
推荐答案
Use GroupBy.transform
instead agg
for return new column with same shape as filtered DataFrame by tail
:
agg_lvl=['A','B','C']
df = df.groupby(agg_lvl,as_index=False).tail(2)
df['G'] = df.groupby(agg_lvl)['G'].transform('mean')
print (df)
A B C D E F G
2 1 1 2 3 2 3 4.5
3 1 1 2 4 5 6 4.5
5 2 1 2 3 4 3 5.0
6 2 1 2 4 5 6 5.0
df = df.groupby(agg_lvl,as_index=False).tail(2).groupby(agg_lvl,as_index=False)['G'].mean()
print (df)
A B C G
0 1 1 2 4.5
1 2 1 2 5.0
这篇关于在python中汇总 pandas 数据时,如何计算每组尾巴的和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!