简体   繁体   English

Pandas - 带总和的唯一列数

[英]Pandas - count of unique columns with sum

I have the following dataframe:我有以下数据框:

   x_1  x_2      x_3    x_4        x_5  ID  cost 
0  159  xyz  883nne2  28h93     lightz  10     2       
1  159  xyz  883nne2  28h93     lightz  10     5       
2  354  abc  94mmm4k             heavy  15     2       
3  354  abc  94mmm4k             heavy  15     1       
4  354  abc  94mmm4k   455h             15     7       
5  354  abc  94mmm4k             super   3     2       
6  354  abc  94mmm4k    53g             10     4   
7  354  abc  94mmm4k    53g             10     4   

I wanted to find the count of every unique combination for columns x_1, x_2, x_3, x_4, x_5, ID and get the sum of those counts and apply the value found in the cost column to get a total sum per unique combination found.我想找到列 x_1、x_2、x_3、x_4、x_5、ID 的每个唯一组合的计数,并获得这些计数的总和,然后应用在成本列中找到的值来获得每个唯一组合的总和。

The end result hopefully will look like this:希望最终结果如下所示:

   x_1  x_2      x_3    x_4        x_5  ID   count sum
0  159  xyz  883nne2  28h93     lightz  10       2   7      
1  354  abc  94mmm4k             heavy  15       2   3      
2  354  abc  94mmm4k   455h             15       1   7
3  354  abc  94mmm4k             super   3       1   2
4  354  abc  94mmm4k    53g             10       2   8

让我们用agggroupby

s=df.groupby(['x_1','x_2','x_3','x_4','x_5','ID'],as_index=False)['cost'].agg(['count','sum'])

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

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