Python 计算特定跨距数据帧中的累积和
我有福勒。数据帧:Python 计算特定跨距数据帧中的累积和,python,pandas,cumulative-sum,Python,Pandas,Cumulative Sum,我有福勒。数据帧: tot_pix Season caap_col lamma kite datetime 2000-01-01 1914.0 2000.0 1.0 1.95025 117.737362 2000-01-04 1914.0 2000.0 1.0 1.95025 117.674177 2000-01
tot_pix Season caap_col lamma kite
datetime
2000-01-01 1914.0 2000.0 1.0 1.95025 117.737362
2000-01-04 1914.0 2000.0 1.0 1.95025 117.674177
2000-01-05 1914.0 2000.0 1.0 1.95025 117.995489
2001-01-04 1914.0 2001.0 1.0 1.95025 118.114809
2001-01-05 1914.0 2001.0 1.0 1.95025 118.160295
在上面的数据框中,我想计算kite
列的累积和。但是,我希望累积总和不跨越季节
值。例如,kite
的累积和输出应如下所示:
tot_pix Season caap_col lamma kite
datetime
1/1/2000 1914 2000 1 1.95025 117.737362
1/4/2000 1914 2000 1 1.95025 235.411539
1/5/2000 1914 2000 1 1.95025 235.669666
1/4/2001 1914 2001 1 1.95025 118.114809
1/5/2001 1914 2001 1 1.95025 236.275104
我可以使用cumsum
命令计算累计总和,如何将其限制为季节的特定跨度?您需要+:
#if not sorted index with column Season
#df = df.sort_index(sort_remaining=True).sort_values('Season')
df['kite'] = df.groupby('Season')['kite'].cumsum()
print (df)
tot_pix Season caap_col lamma kite
datetime
2000-01-01 1914.0 2000.0 1.0 1.95025 117.737362
2000-01-04 1914.0 2000.0 1.0 1.95025 235.411539
2000-01-05 1914.0 2000.0 1.0 1.95025 353.407028
2001-01-04 1914.0 2001.0 1.0 1.95025 118.114809
2001-01-05 1914.0 2001.0 1.0 1.95025 236.275104