python - 使用 pandas 根据每日百分比返回计算累积股价指数

标签 python dataframe

我正在尝试计算一个从 100 开始并根据每日 yield 累积变化的金融股票价格指数。下面是我目前实现这个过程的代码;但是,我想知道是否有一种更 Pythonic 的方法可以在不使用 for 循环方法的情况下实现相同的结果。

df_stock_returns = df_stock_prices.pct_change(1)

df_stock_idx = df_stock_returns.copy(deep=True)

df_stock_idx.iloc[0] = 100

# Calculated the index over time
for idx, row in enumerate(df_stock_idx.iterrows()):
    if idx == 0:
        df_stock_idx.iloc[0] = 100
    else:
        df_stock_idx.iloc[idx] = (1 + df_stock_returns.iloc[idx]) * df_stock_idx.iloc[idx - 1]

下面是 df_stock_returns 的样子:

enter image description here

这就是我想要返回的内容

enter image description here

最佳答案

请尽量不要在您的问题中使用图片。我们希望能够从问题中复制数据以获得答案:)

从雅虎获取一些股票,过滤收盘价,然后调整到变化的百分比。加 1,这样你就有了可以正确乘法的百分比,

import yfinance as yf
df = yf.download("AAPL F GM AEO GDDY ORCL", start="2017-01-01", end="2018-12-31")df df = df.loc[:, "Close"].pct_change()+1
print(df.head(10))

                AAPL       AEO         F      GDDY        GM      ORCL
Date                                                                  
2017-01-03       NaN       NaN       NaN       NaN       NaN       NaN
2017-01-04  0.998881  1.011643  1.046068  0.989082  1.055192  1.004929
2017-01-05  1.005085  0.971867  0.969628  0.985848  0.981127  0.997419
2017-01-06  1.011148  0.950658  0.999217  1.015791  0.989008  0.995083
2017-01-09  1.009160  1.035986  0.989812  1.000000  1.000556  1.015085
2017-01-10  1.001008  1.018036  1.017419  0.995195  1.037212  0.990520
2017-01-11  1.005373  0.991470  0.985992  1.001704  1.016064  1.011640
2017-01-12  0.995825  1.001985  0.993686  0.975617  0.988406  1.002301
2017-01-13  0.998239  1.004624  1.003177  1.022377  0.995468  1.001531
2017-01-17  1.008065  1.005260  0.998416  1.000569  0.999197  0.995925

将初始值设置为 100 除以股票总数。

df.iloc[0, :] = 100 / df.shape[1]

print(df.head(3))

                AAPL        AEO          F       GDDY         GM       ORCL
Date                                                                        
2017-01-03  16.666667  16.666667  16.666667  16.666667  16.666667  16.666667
2017-01-04   0.998881   1.011643   1.046068   0.989082   1.055192   1.004929
2017-01-05   1.005085   0.971867   0.969628   0.985848   0.981127   0.997419

使用 cumprod 创建索引列:

df['INDEX'] = df.cumprod(axis=0).sum(axis = 1)

print(df['INDEX'])

Date
2017-01-03    100.000000
2017-01-04    101.763241
2017-01-05    100.235598
2017-01-06     99.585180
2017-01-09    100.388884
2017-01-10    101.384499
2017-01-11    101.604947
2017-01-12    100.888649
2017-01-13    101.291541
2017-01-17    101.414995
2017-01-18    101.134378
2017-01-19    100.745957
2017-01-20    100.918941
2017-01-23    100.257569
2017-01-24    101.240530
2017-01-25    103.360326
2017-01-26    102.102662
2017-01-27    101.783095
2017-01-30    101.194277
2017-01-31    101.435755
2017-02-01    102.082693
2017-02-02    101.932515
2017-02-03    102.844573
2017-02-06    103.331255
2017-02-07    101.985985
2017-02-08    102.214225
2017-02-09    102.925301
2017-02-10    103.557639
2017-02-13    103.699594
2017-02-14    105.191437
                 ...    
2018-11-15    131.222953
2018-11-16    130.297599
2018-11-19    125.998103
2018-11-20    123.824591
2018-11-21    124.341525
2018-11-23    124.119347
2018-11-26    127.823201
2018-11-27    126.330787
2018-11-28    130.525408
2018-11-29    129.277160
2018-11-30    130.170444
2018-12-03    133.219588
2018-12-04    127.571927
2018-12-06    126.183687
2018-12-07    122.360237
2018-12-10    123.354318
2018-12-11    122.167353
2018-12-12    124.628647
2018-12-13    122.531875
2018-12-14    120.621529
2018-12-17    118.664316
2018-12-18    120.195025
2018-12-19    119.267250
2018-12-20    117.975604
2018-12-21    113.388324
2018-12-24    111.691453
2018-12-26    118.762111
2018-12-27    118.867354
2018-12-28    118.508261
2018-12-31    119.598802
Name: INDEX, Length: 502, dtype: float64

关于python - 使用 pandas 根据每日百分比返回计算累积股价指数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57001398/

相关文章:

python - 获取seaborn因子图中箱线图的样本大小

python - 如何在 Pandas bool 值缩减中获取导致 True 的列名

python - 如何从转置格式将 .txt 文件读入 pandas DataFrame

python - 使 pyplot 比 gnuplot 更快

python - 使用 matplotlib 在另一个轴中嵌入多个插入轴

python - 使用 Pandas 将多个数据框合并为一个

使用分层索引将 Pandas 系列连接回源 DataFrame

python - 将行移动到另一个数据帧时如何加快 Pandas contains 的速度

python - 为什么看似有效的 Python 脚本在通过 Windows 任务计划程序启动时无法运行?

python - 将 python venv 与自定义库结合使用