Skip to content
Advertisement

Check for max value of iterative sum in pandas

I have a pandas dataframe with some data:

0   -0.000601
1    0.000001
2    0.000000
3    0.000007
4    0.000300
5   -0.000300
Name: measure, dtype: float64

I’d like to sum them, one at a time, and at every summation check the totalt sum, an then extract the highest value achieved of the total sum.

So the first sum would be element 0, with a value of -0.000601, the send would be -0.000601 + 0.000001=-0.0006, and this would be higher than the first sum (in other words, just element 0), so that value would be the max value up to that point.

I suppose a for loop could do the job but I’d prefer to do something pythonic like measure.loc[0:4, 'measure'].max() but this just checks the max value of the totalt sum.

Advertisement

Answer

I believe you need a cumulative sum and then to locate the max.

>>> df = pd.DataFrame({'a':[-0.000601, 0.000001, 0.000000, 0.000007, 0.000300, -0.000300]})
>>> df

out:
          a
0 -0.000601
1  0.000001
2  0.000000
3  0.000007
4  0.000300
5 -0.000300

>>> df['b'] = df.cumsum()
>>> df

out:
          a         b
0 -0.000601 -0.000601
1  0.000001 -0.000600
2  0.000000 -0.000600
3  0.000007 -0.000593
4  0.000300 -0.000293
5 -0.000300 -0.000593

>>> df[df['b'] == df['b'].max()]

out:
        a         b
4  0.0003 -0.000293
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement