首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Split-Apply-Combine聚合Pandas数据帧中的行

Split-Apply-Combine聚合Pandas数据帧中的行
EN

Stack Overflow用户
提问于 2020-02-24 13:41:22
回答 2查看 106关注 0票数 0

我正在尝试对以下pandas数据帧运行split-apply-combine。我希望为每个唯一的客户代码显示col1值小于或等于0的行的计数,以及col1值大于0的行的计数。

因此,此表:

代码语言:javascript
复制
    cust_code   col1    col2
0   113231413   -10     2795.19 
1   113231413   20      1485.76 
2   113231413   25      6201.18 
3   113231413   -25     1502.40 
4   526242422   -23     6470.12 
5   526242422   0       14011.28    
6   414314134   0       12426.78    
7   414314134   26      12104.77

变成:

代码语言:javascript
复制
           cust_code    count1 count2   
0          113231413      2      2      
1          526242422      2      0      
2          414314134      1      1

我知道第一步是使用groupby,但我不确定下一步该做什么:

代码语言:javascript
复制
count = df.groupby('cust_code')['cust_code'].count()
EN

回答 2

Stack Overflow用户

发布于 2020-02-24 14:02:10

对于pandas >= 0.25,请尝试:

代码语言:javascript
复制
df.groupby('cust_code')['col1'].agg(count1=(lambda s: s.loc[s <= 0].count()), 
                                    count2=(lambda s: s.loc[s > 0].count())).reset_index()

   cust_code  count1  count2
0  113231413       2       2
1  414314134       1       1
2  526242422       2       0

或者对于较老的熊猫版本:

代码语言:javascript
复制
df1 = df.groupby('cust_code').agg(
          {'col1': [lambda s: s.loc[s <= 0].count(),
                    lambda s: s.loc[s > 0].count()]
          }
      ).reset_index()
df1.columns=['cust_code','count1','count2']
df1
   cust_code  count1  count2
0  113231413       2       2
1  414314134       1       1
2  526242422       2       0
票数 1
EN

Stack Overflow用户

发布于 2020-02-24 15:46:15

如果性能很重要,永远不要按组过滤,因为如果有很多组,速度会很慢。更好的方法是创建由转换为整数的掩码填充的新列,并聚合sum

代码语言:javascript
复制
df1 = (df.assign(col1=df['col1'].le(0).astype(int),
               col2=df['col1'].gt(0).astype(int))
        .groupby('cust_code',sort=False)[['col1','col2']]
        .sum())
print   (df)1
           col1  col2
cust_code            
113231413     2     2
526242422     2     0
414314134     1     1
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60370163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档