Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pulling stock information using pandas datareader

I am using pandas datareader to pull stock information for a given range of dates. For example:

import pandas_datareader.data as web
import datetime as dt

start = dt.datetime(2018,3,26)
end = dt.datetime(2018,3,29)

web.DataReader('IBM','yahoo', start, end).reset_index()

This returns the following dataframe for IBM:

enter image description here

This contains the information I am looking for, but I would like to automatically iterate through multiple stock tickers (instead of manually changing the stock ticker). Ideally I could loop this code through a list of desired stock tickers.

like image 421
Brian Avatar asked Apr 02 '18 01:04

Brian


People also ask

What is the use of pandas Datareader?

The Pandas datareader is a sub package that allows one to create a dataframe from various internet datasources, currently including: Yahoo! Finance. Google Finance.


2 Answers

Here is another way, creating your dataframe directly:

tickers = ['IBM','AAPL']

df = pd.concat([web.DataReader(ticker,'morningstar', start, end) for ticker in tickers]).reset_index()

Which returns:

  Symbol       Date   Close      High     Low     Open    Volume
0    IBM 2018-03-26  153.37  153.6570  150.28  151.210   4103904
1    IBM 2018-03-27  151.91  154.8697  151.16  153.950   3883745
2    IBM 2018-03-28  152.52  153.8600  151.89  152.070   3664826
3    IBM 2018-03-29  153.43  153.8900  151.08  153.070   3419959
4   AAPL 2018-03-26  172.77  173.1000  166.44  168.070  37541236
5   AAPL 2018-03-27  168.34  175.1500  166.92  173.680  40922579
6   AAPL 2018-03-28  166.48  170.0200  165.19  167.250  41668545
7   AAPL 2018-03-29  167.78  171.7500  166.90  167.805  38398505
like image 164
sacuL Avatar answered Oct 02 '22 07:10

sacuL


I think that this might be what you are looking for.

import pandas_datareader.data as web
import datetime as dt

start = dt.datetime(2018,3,26)
end = dt.datetime(2018,3,29)
tickers = ['IBM', 'FB']
data = {}
for t in tickers:
   data[t] = web.DataReader(t,'morningstar', start, end).reset_index()
like image 24
Stoicas Avatar answered Oct 02 '22 06:10

Stoicas