无法获取数据,使用包 yfinance 按格式分组下载到 pandas 数据框,仅适用于我的子列表中的特定代码

Not able to get data, downloaded in group by format using package yfinance, into pandas dataframe only for specific tickers in my sub list

我正在使用 python 包 yfinance 下载数据,下面是我为此使用的代码:

# pip install yfinance

import yfinance as yf

# list of tickers for which data is to be downloaded
myLst = ['TSLA', 'MSFT', 'FB', 'IBM', 'AAPL', 'WFC', 'BAC', 'INTC', 'PRLAX', 'QASGX', 'HISFX']

# from which date?
start_date = '2018-01-01'

# to which date?
end_date = '2019-12-31'

# download the data
mydf = yf.download(myLst, start = start_date, end = end_date, group_by = "ticker")

这个 returns 数据框中的数据如下所示:

但我无法将这些数据放入这样的数据框中:

虽然我尝试过这样的事情:

assets = ['IBM', 'MSFT', 'FB']

assetsDF = pd.DataFrame({
    a: {x['Close'] for x in mydf[a]['Close']} for a in assets
})

但我知道我在这里不正确。有什么解决办法吗?

正如这里的回答:pandas dataframe select columns in multiindex

mydf.iloc[:, mydf.columns.get_level_values(1)=='Close']

@EDIT:删除无用的列级别

mydf.columns = mydf.columns.droplevel(1)