spa*_*boy 3 yahoo-finance pandas yfinance
如何将以下代码的输出转换为数据框?
fund = Ticker('hasgx')
fund.fund_holding_info
Run Code Online (Sandbox Code Playgroud)
我从下面开始,
tickers = ['HACAX']
fund = Ticker(tickers)
fund = fund.fund_holding_info
fund = pd.DataFrame(fund).reset_index().rename(columns={'index': 'Header'})
fund = fund[fund["Header"] == "holdings"]
Run Code Online (Sandbox Code Playgroud)
输出为,
所有这些如何转换为包含“控股”行和列的数据框?
返回的信息是字典的形式,所以你可以指定你想知道的项目并将它们带入数据框以获得期望的格式。
import yahooquery as yq
fund = yq.Ticker('hasgx')
fund.fund_holding_info.keys()
#dict_keys(['hasgx'])
import pandas as pd
df = pd.DataFrame(fund.fund_holding_info['hasgx']['holdings'])
df
symbol holdingName holdingPercent
0 WMS Advanced Drainage Systems Inc 0.0257
1 FCNCA First Citizens BancShares Inc Class A 0.0231
2 TPTX Turning Point Therapeutics Inc 0.0230
3 HXL Hexcel Corp 0.0222
4 EYE National Vision Holdings Inc 0.0220
5 SIMO Silicon Motion Technology Corp ADR 0.0208
6 ATKR Atkore Inc 0.0202
7 PCRX Pacira BioSciences Inc 0.0200
8 TKR The Timken Co 0.0197
9 XLRN Acceleron Pharma Inc 0.0195
Run Code Online (Sandbox Code Playgroud)
如何为每个库存单位制作数据框
import yahooquery as yq
import pandas as pd
stocks = ['hasgx','vinix']
for s in stocks:
fund = yq.Ticker(s)
locals()[s] = pd.DataFrame(fund.fund_holding_info[s]['holdings'])
Run Code Online (Sandbox Code Playgroud)
将它们存储在同一个数据框中
import yahooquery as yq
import pandas as pd
stocks = ['hasgx','vinix']
df = pd.DataFrame()
for s in stocks:
fund = yq.Ticker(s)
tmp = pd.DataFrame(fund.fund_holding_info[s]['holdings'])
tmp['ticker'] = s
df = df.append(tmp, ignor_index=True)
df = df[['ticker', 'symbol', 'holdingName', 'holdingPercent']]
ticker symbol holdingName holdingPercent
0 hasgx WMS Advanced Drainage Systems Inc 0.0257
1 hasgx FCNCA First Citizens BancShares Inc Class A 0.0231
2 hasgx TPTX Turning Point Therapeutics Inc 0.0230
3 hasgx HXL Hexcel Corp 0.0222
4 hasgx EYE National Vision Holdings Inc 0.0220
5 hasgx SIMO Silicon Motion Technology Corp ADR 0.0208
6 hasgx ATKR Atkore Inc 0.0202
7 hasgx PCRX Pacira BioSciences Inc 0.0200
8 hasgx TKR The Timken Co 0.0197
9 hasgx XLRN Acceleron Pharma Inc 0.0195
10 vinix AAPL Apple Inc 0.0592
11 vinix MSFT Microsoft Corp 0.0562
12 vinix AMZN Amazon.com Inc 0.0406
13 vinix FB Facebook Inc Class A 0.0229
14 vinix GOOGL Alphabet Inc Class A 0.0202
15 vinix GOOG Alphabet Inc Class C 0.0197
16 vinix BRK.B Berkshire Hathaway Inc Class B 0.0145
17 vinix TSLA Tesla Inc 0.0144
18 vinix NVDA NVIDIA Corp 0.0137
19 vinix JPM JPMorgan Chase & Co 0.0130
Run Code Online (Sandbox Code Playgroud)