简体   繁体   English

带有聚合的 Python itertools groupby

[英]Python itertools groupby with aggregate

I am trying to group on a column based on the sequence it appears (timestamp) and simultaneously finding aggregate (mean) on the other variables within the small group.我试图根据它出现的顺序(时间戳)对列进行分组,并同时在小组内的其他变量上找到聚合(平均值)。 I can successfully group it but unable to aggregate我可以成功分组但无法聚合

Here is my sample input:这是我的示例输入:

Date        T/F X1
12/02/19    T   10
12/02/19    T   20
12/02/19    F   15
12/02/19    T   12
12/03/19    F   10
12/03/19    F   20
12/03/19    T   30
12/04/19    T   40

Expected O/P预期产量

Date        T/F X1  Count
12/02/19    T   15  2
12/02/19    F   15  1
12/02/19    T   12  1
12/03/19    F   15  2
12/03/19    T   35  2

Here is the code I am using, which groups and give me the count for each group, how do I get the avg of X1 as well, within that group这是我正在使用的代码,哪些组并给我每个组的计数,我如何在该组内获得 X1 的平均值

import itertools 
for (key,group) in itertools.groupby(df['T/F']): 
    print (key, len(list(group))) 

Thanks for the help!谢谢您的帮助!

You can use the function groupby :您可以使用函数groupby

df1 = df.assign(Count=np.nan).\
groupby(df['T/F'].ne(df['T/F'].shift()).cumsum(), as_index=False).\
agg({'Date': 'first', 'T/F': 'first', 'X1': 'mean', 'Count': 'size'})

print(df1)

Output:输出:

       Date T/F  X1  Count
0  12/02/19   T  15      2
1  12/02/19   F  15      1
2  12/02/19   T  12      1
3  12/03/19   F  15      2
4  12/03/19   T  35      2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM