根据列值自定义groupby [英] Custom groupby based on column values

查看:95
本文介绍了根据列值自定义groupby的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出这个数据框:

  C 
索引
0 9
1 0
2 1
3 5
4 0
5 1
6 2
7 20
8 0




  • >组1具有 [9,0]

  • 组2具有 [1,5,0]

  • 组3具有 [1,2,20,0]



这个想法是找出所有以0结尾的序列并将它们组合在一起。序列的大小可能不同,最后的序列可能不以0结尾。第一个元素永远不会为0。



我的最终结果如下所示:

p>

  C_new 
9
6
23

在哪里找到这些组,然后对它们进行求和。

解决方案

使用 groupby

code>系列:

  print (df ['C']。shift(1).eq(0).cumsum())
0 0
1 0
2 1
3 1
4 1
5 2
6 2
7 2
8 2
名称:C,dtype:int32

df = df [ C']。groupby(df ['C']。shift(1).eq(0).cumsum())。sum()
print(df)
C
0 9
1 6
2 23
名称:C,dtype:int64


Given this dataframe:

        C
index    
0       9
1       0
2       1
3       5
4       0
5       1
6       2
7       20
8       0

How can I split this into groups such that

  • Group 1 has [9, 0],
  • Group 2 has [1, 5, 0],
  • Group 3 has [1, 2, 20, 0]?

The idea is to find all sequences that terminate with 0 and group them together. The sequences can vary in size and and the last sequence may not terminate with 0. The first element will never be 0.

My end result looks something like this:

C_new
9
6
23

Where I find these groups and then sum them.

解决方案

Use groupby by Series:

print (df['C'].shift(1).eq(0).cumsum())
0    0
1    0
2    1
3    1
4    1
5    2
6    2
7    2
8    2
Name: C, dtype: int32

df = df['C'].groupby(df['C'].shift(1).eq(0).cumsum()).sum()
print (df)
C
0     9
1     6
2    23
Name: C, dtype: int64

这篇关于根据列值自定义groupby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆