计算 pandas 系列的运行(累积)最大值 [英] Compute the running (cumulative) maximum for a series in pandas

查看:59
本文介绍了计算 pandas 系列的运行(累积)最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出:

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

推荐答案

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

这篇关于计算 pandas 系列的运行(累积)最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆