简体   繁体   中英

How to append/concat pandas dataframes of stock prices into one large dataframe

I have a list of 200 tickers which I'm trying to to import from csv into one large dataframe like this:

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)

My concat code does not work, and it only shows prices for 'SPY'. Could someone please tell how to get it for 'AAPL' as well? I have a list of 200 stocks or so, want to merge it all together

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)

Outputs

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM