【问题标题】:Simple return index in Pandas with .shiftPandas 中使用 .shift 的简单返回索引
【发布时间】:2019-12-29 00:14:03
【问题描述】:

我想计算一个简单的指数系列,但不知何故它不起作用。数据框有两列:索引列和要添加的百分比变化:

      Indice  PerChange
1        1   0.024412
2        1   0.036074
3        1   0.027329
4        1   0.027981

这是我的代码:

df3["Indice"] = df3["Indice"] * (df3["PerChange"] + df3["Indice"].shift(1))
df3.at[1, 'Indice'] = 1

我得到以下输出:

 Indice  PerChange
1   1.000000   0.024412
2   1.036074   0.036074
3   1.027329   0.027329
4   1.027981   0.027981

但是,我想看到的是:

 Indice  PerChange
1   1.000000   0.024412
2   1.036074   0.036074
3   1.064388   0.027329
4   1.094171   0.027981

【问题讨论】:

    标签: pandas indexing time-series


    【解决方案1】:

    你基本上是在找累积积:

    s = df['PerChange'] + 1
    s.at[1] = 1
    
    df['Indice'] = s.cumprod()
    

    【讨论】:

    • 这是我寻找的缺失函数!谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 2019-11-14
    相关资源
    最近更新 更多