如何相互附加/协调股价

时间:2018-10-10 04:40:16

标签: python pandas stocks

我有一个代码列表,它们是通过和csv导入的,然后是这样的数组

tickers = ['SPY', 'AAPL',]

for ticker in tickers:
    start = datetime.datetime(2017, 1, 1)
    end = datetime.datetime(2018, 10, 3)
    ticker1 = data.DataReader(ticker,'iex', start, end)

    stocks_prices = pd.concat([ticker1],axis=1,keys=tickers)

我的concat代码不起作用。并且仅显示SPY的价格。有人可以告诉我如何也获得AAPL吗?我有200支左右的股票清单,想要将它们全部合并在一起

谢谢

1 个答案:

答案 0 :(得分:0)

import datetime
import pandas_datareader.data as web
import pandas as pd
tickers = ['SPY', 'NVTR',]

frames=[]
for ticker in tickers:
    start = datetime.datetime(2018, 10, 1)
    end = datetime.datetime(2018, 10, 3)
    ticker1 = web.DataReader(ticker,'iex', start, end)
    frames.append(ticker1)

stocks_prices = pd.concat(frames,keys=tickers)
print(stocks_prices)

输出

                   open     high     low   close    volume
     date                                                 
SPY  2018-10-01  292.11  292.930  290.98  291.73  62078937
     2018-10-02  291.56  292.355  291.14  291.56  47258227
     2018-10-03  292.74  293.210  291.32  291.72  64694594
NVTR 2018-10-01   22.01   24.890   21.89   24.10    492424
     2018-10-02   24.39   24.790   23.50   23.68    336636
     2018-10-03   23.68   24.280   22.76   23.97    400894