数据帧行的总和 一列包含一个子字符串



我有这个数据帧:

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.othertext2sometext(因此它是第一个XXX.othertext2+ 所有XXX.othertext3的总和(。生成的行值InfoXXX.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()

如有必要,您可以使用正则表达式对其进行更多优化。

相关内容

最新更新