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

独自空忆成欢 提交于 2019-11-30 17:42:24

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/39498729/compute-the-running-cumulative-maximum-for-a-series-in-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!