【问题标题】:Get original values from cumulative sum从累积和中获取原始值
【发布时间】:2014-11-11 16:52:38
【问题描述】:
>>> test.val.cumsum()
0    11
1    13
2    56
3    60
4    65
Name: val, dtype: int64

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

【问题讨论】:

    标签: python pandas dataframe cumulative-sum


    【解决方案1】:

    您可以使用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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-01
      • 1970-01-01
      • 2012-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多