我正在尝试计算一个金融股票价格指数,该指数从100开始,并根据每日收益累计变化。下面是实现此过程所需的当前代码;但是,我想知道是否还有一种更Python的方法可以在不使用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的样子:
这就是我要退货的
答案 0 :(得分:1)
请尽量不要在问题中使用图片。我们希望能够从问题中复制数据以获得答案:)
从Yahoo抓取一些股票,过滤掉收盘,然后调整百分比变化。加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