具有DataReader中多个列的DataFrame

问题描述 投票:0回答:1

下面的代码是我正在使用Yahoo的Adj Close数据创建数据框的方法。我收到cannot reindex from a duplicate axis的错误。

tickers = ['MSFT','AAPL','^IRX','UNH','MA','FB','CRM','GS','TM','TSLA']

content = pd.DataFrame()

for t in tickers:
        content[t] = pdr.DataReader(t, data_source='yahoo', start='1990-1-1')['Adj Close']

content.head()
dataframe finance yahoo-finance
1个回答
0
投票

签出yahooquery

安装后,您可以执行以下操作:

from yahooquery import Ticker

tickers = ['MSFT','AAPL','^IRX','UNH','MA','FB','CRM','GS','TM','TSLA']

t = Ticker(tickers)
df = t.history(start='1990-01-01')['adjclose']

这将返回具有MultiIndex(包含符号和日期)的数据框。

df.index.unique(level='symbol')
Index(['MSFT', 'AAPL', '^IRX', 'UNH', 'MA', 'FB', 'CRM', 'GS', 'TM', 'TSLA'], dtype='object', name='symbol')
© www.soinside.com 2019 - 2024. All rights reserved.