我正在尝试使用 yfinance 下载股票数据信息。目前,我可以使用
yf.download
成功下载单个股票代码,它返回一个包含我可以使用的信息的数据帧。此 API 还支持通过单个 API 调用一次下载多个股票代码。由于下载 API 的速率限制,我需要一次查询所有股票代码,而不是调用每个股票代码。
返回的数据帧将所有信息聚集在一起。如何将
multiple_df
数据帧分成单独的数据帧,就像拉动单个股票一样?我已经研究过如何处理使用 yfinance 下载的多级列名称,但它并没有像使用单个代码调用 API 那样构造单独的数据帧。
GOOGL 的单一股票代码示例
Adj Close Close High Low Open Volume
Date
2022-12-13 95.28 95.63 99.53 95.03 97.76 40593700
2022-12-14 94.73 95.07 96.87 93.60 95.20 28733600
2022-12-15 90.53 90.86 93.64 90.01 93.13 40107000
2022-12-16 89.93 90.26 91.33 89.52 90.76 58011800
2022-12-19 88.12 88.44 90.56 88.21 90.26 29493000
... ... ... ... ... ... ...
2024-12-06 174.51 174.71 175.08 171.86 172.03 21462400
2024-12-09 175.37 175.37 176.26 173.65 173.96 25389600
2024-12-10 185.17 185.17 186.36 181.05 182.85 54813000
2024-12-11 195.40 195.40 195.61 184.85 185.31 67894100
2024-12-12 191.96 191.96 195.18 191.71 194.96 32460315
多个下载数据帧的当前输出
Ticker NVDA ... MSFT
Price Open High Low Close Adj Close ... High Low Close Adj Close Volume
Date ...
2022-12-13 18.531000 18.790001 17.711000 18.072001 18.058865 ... 263.920013 253.070007 256.920013 252.771729 42196900
2022-12-14 17.987000 18.252001 17.420000 17.674000 17.661152 ... 262.589996 254.309998 257.220001 253.066925 35410900
2022-12-15 17.146000 17.320000 16.700001 16.952000 16.939678 ... 254.199997 247.339996 249.009995 244.989441 35560400
2022-12-16 16.864000 17.041000 16.410000 16.570999 16.558958 ... 249.839996 243.509995 244.690002 240.739197 86102000
2022-12-19 16.572001 16.608999 16.145000 16.254000 16.242184 ... 245.210007 238.710007 240.449997 236.567657 29696400
... ... ... ... ... ... ... ... ... ... ... ...
2024-12-06 144.600006 145.699997 141.309998 142.440002 142.440002 ... 446.100006 441.769989 443.570007 443.570007 18821000
2024-12-09 138.970001 139.949997 137.130005 138.809998 138.809998 ... 448.329987 440.500000 446.019989 446.019989 19144400
2024-12-10 139.009995 141.820007 133.789993 135.070007 135.070007 ... 449.619995 441.600006 443.329987 443.329987 18469500
2024-12-11 137.360001 140.169998 135.210007 139.309998 139.309998 ... 450.350006 444.049988 448.989990 448.989990 19200200
2024-12-12 137.100006 138.410004 135.800003 137.339996 137.339996 ... 456.164795 449.130005 449.559998 449.559998 20719891
每个股票行情数据帧分开的所需输出
separated_df = {'AAPL': aapl_df,
'MSFT': msft_df,
'NVDA': nvda_df}
MultiIndex 数据框还有额外的
names=['Price', 'Ticker']
列,我似乎无法摆脱
代码
import yfinance as yf
import pandas as pd
single = ['GOOGL']
df = yf.download(single,
period='2y',
interval='1d',
multi_level_index=False)
print(df)
tickers = ['AAPL', 'MSFT', 'NVDA']
multiple_df = yf.download(tickers,
period='2y',
interval='1d',
group_by='Ticker',
multi_level_index=False)
print(multiple_df)
好的知道怎么做了。 在 MultiIndex 上建立索引
multiple_df.index.name = None
data = {idx: gp.T for idx, gp in multiple_df.T.groupby(level=0)}
separated_df = {}
for ticker in data:
new_df = data[ticker][ticker]['Close High Low Open Volume'.split()]
new_df.columns.name = None
adj_close = data[ticker][ticker]['Adj Close']
new_df.insert(0, 'Adj Close', adj_close)
separated_df[ticker] = new_df
print(separated_df)