我有这个数据帧:
df1:
Date Value Info
1 1 XXX.othertext2
1 4 somerandomtext
1 2 XXX.othertext2
1 3 XXX.othertext3
1 2 XXX.othertext3
1 1 XXX.othertext2
1 1 XXX.othertext3
2 6 somerandomtext
2 9 XXX.othertext2
我想按以XXX.othertext2
开头的相同Date
对行进行求和,直到新的XXX.othertext2
或sometext
(因此它是第一个XXX.othertext2
+ 所有XXX.othertext3
的总和(。生成的行值Info
将XXX.othertext2
:
newdf:
Date Value Info
1 1 XXX.othertext2
1 4 somerandomtext
1 7 XXX.othertext2
1 2 XXX.othertext2
2 6 sometext
2 9 XXX.othertext2
这是一个选项,带有自定义grouper
:
grouper = ((b.Info.str.contains('some')) | (b.Info == 'XXX.othertext2')).cumsum()
b.groupby(['Date', grouper]).sum().reset_index()
如有必要,您可以使用正则表达式对其进行更多优化。