I have a pandas dataframe with some data:
JavaScriptx810 -0.000601
21 0.000001
32 0.000000
43 0.000007
54 0.000300
65 -0.000300
7Name: measure, dtype: float64
8
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.
JavaScript
1
30
30
1
>>> df = pd.DataFrame({'a':[-0.000601, 0.000001, 0.000000, 0.000007, 0.000300, -0.000300]})
2
>>> df
3
4
out:
5
a
6
0 -0.000601
7
1 0.000001
8
2 0.000000
9
3 0.000007
10
4 0.000300
11
5 -0.000300
12
13
>>> df['b'] = df.cumsum()
14
>>> df
15
16
out:
17
a b
18
0 -0.000601 -0.000601
19
1 0.000001 -0.000600
20
2 0.000000 -0.000600
21
3 0.000007 -0.000593
22
4 0.000300 -0.000293
23
5 -0.000300 -0.000593
24
25
>>> df[df['b'] == df['b'].max()]
26
27
out:
28
a b
29
4 0.0003 -0.000293
30