计算 pandas 中一系列的 运行(累积)最大值

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

鉴于:

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

我想添加另一列,它是每个索引从头开始的最大值。例如,所需的列为:

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

我尝试使用 pandas 滚动函数,但 window 似乎不能动态

使用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