我有一个类似于这个的数据框,我想要获得"Number"列的累加和,直到"Name"列等于"AAAA"为止。 因此,我想要在每个'AAAA'之间获得"Number"列的累积和。所以每次在列中出现'AAAA'时,求和就重新开始。 有没有一种方法可以做到这一点?
data = {'Name': ['AAAA','B','C','D','E','AAAA','O','C','D','E','AAAA','D', 'C','D','E','AAAA','B','C','D','E','AAAA','L','M'],
'Number': [7,8,9,10,1,1,2,34,5,6,7,8,9,10,1,1,7,8,2,3,5,6,7]
}
df = pd.DataFrame (data, columns = ['Name','Number'])
df['Sum_Cummulative']=df['Number'].cumsum()