简体   繁体   English

GroupBy中的熊猫累积总和

[英]Pandas Cumulative Sum in GroupBy

I have a time series data with symbol and their respective values at a particular time. 我有一个带符号的时间序列数据及其在特定时间的各自值。

index,symbol,value
01:00,A,10
01:00,B,15
01:01,A,15
01:01,B,25
01:02,A,30
01:02,B,45

Now I want create a 4th column, which has cumulative value on time series basis for each symbol but from each cumulative row, the first row value would be subtracted for each symbol respectively 现在我想创建第4列,其中每个符号的时间序列累计值,但从每个累积行,将分别减去每个符号的第一行值

index,symbol,value,adjustedCumulativeSum
01:00,A,10,0
01:00,B,15,0
01:01,A,15,15
01:01,B,25,25
01:02,A,30,45
01:02,B,45,70

I know how to do normal cumulative sum 我知道如何做正常的累计和

df = df.reset_index().sort_values(['index','symbol'])
df['cumlativesum'] = df.groupby('symbol')['value'].cumsum()
df = df.set_index('index')

But do I deduct row 0 value from all cumulative sums? 但是,我是否从所有累计金额中扣除第0行的值?

Use groupby with custom function with cumsum and substract first value selected by iat : groupby与带有cumsum自定义函数一起cumsum并减去iat选择的第一个值:

df['adjustedCumulativeSum']=df.groupby('symbol')['value'].apply(lambda x:x.cumsum()-x.iat[0])
print (df)
   index symbol  value  adjustedCumulativeSum
0  01:00      A     10                      0
1  01:00      B     15                      0
2  01:01      A     15                     15
3  01:01      B     25                     25
4  01:02      A     30                     45
5  01:02      B     45                     70

You can subtract the first value (extracted with .iat[0] ) for each group in a transform function: 您可以在transform函数中为每个组减去第一个值(使用.iat[0]提取):

df['cumlativesum'] = df.groupby('symbol')['value'].transform(lambda g: g.cumsum()-g.iat[0])
df = df.set_index('index')

df
#      symbol   value   cumlativesum
#index          
#01:00      A      10          0
#01:00      B      15          0
#01:01      A      15         15
#01:01      B      25         25
#01:02      A      30         45
#01:02      B      45         70
df.groupby('sy').val.apply(lambda x : x.cumsum()-x.values.tolist()[0])
Out[907]: 
0     0
1     0
2    15
3    25
4    45
5    70
Name: val, dtype: int64

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

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