从累积总和中获取原始值

时间:2014-11-11 16:52:38

标签: python pandas dataframe cumulative-sum

>>> test.val.cumsum()
0    11
1    13
2    56
3    60
4    65
Name: val, dtype: int64

如何从累积总和中获取原始值?我必须得到[11,2,43,4,5]

1 个答案:

答案 0 :(得分:2)

您可以使用diff()系列方法(使用fillna替换系列中的第一个值):

>>> s = pd.Series([11, 13, 56, 60, 65])
>>> s.diff().fillna(s)
0    11
1     2
2    43
3     4
4     5
dtype: float64