Get original values from cumulative sum

若如初见. 提交于 2020-05-13 23:34:32

问题


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

How do I get the original values from the cumulative sum? I will have to get [11,2,43,4,5]


回答1:


You could use the diff() Series method (with fillna to replace the first value in the series):

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


来源:https://stackoverflow.com/questions/26870116/get-original-values-from-cumulative-sum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!