how to calculate sum|mean|median for tail of each group when pandas data aggregated in python

强颜欢笑 提交于 2019-12-11 02:25:40

问题


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

here agg_lvl=['A','B','C']

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:

expected output for mean:

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.

Thanks in advance.


回答1:


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

EDIT:

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


来源:https://stackoverflow.com/questions/52183552/how-to-calculate-summeanmedian-for-tail-of-each-group-when-pandas-data-aggrega

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!