Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compute the running (cumulative) maximum for a series in pandas

Given:

d = {
    'High': [954,
             953,
             952,
             955,
             956,
             952,
             951,
             950,
            ]
}
df = pandas.DataFrame(d)

I want to add another column which is the max at each index from the beginning. For example the desired column would be:

'Max': [954, 
        954, 
        954, 
        955, 
        956, 
        956, 
        956, 
        956]

I tried with a pandas rolling function but the window cannot be dynamic it seems

like image 374
Tony Abboud Avatar asked Sep 14 '16 20:09

Tony Abboud


1 Answers

Use cummax

df.High.cummax()

0    954
1    954
2    954
3    955
4    956
5    956
6    956
7    956
Name: High, dtype: int64

df['Max'] = df.High.cummax()
df

enter image description here

like image 57
piRSquared Avatar answered Oct 05 '22 10:10

piRSquared