0

I'm trying to store stock data (Open, High, Low, Close, Volume), pulled by pandas_datareader, into 5 distinct lists named accordingly. I am new to Python and am wondering where I am going wrong. I got it to cycle through a one-dimensional list of integer values and assign them to each list, but am unsure of how to handle the additional dimension of the f.head output. I have twice gotten a traceback error indicating index values out of range, but know that I've made a mistake beyond simple index range.

Open, High, Low, Close, Vol = [], [], [], [], []
col_data = [Open, High, Low, Close, Vol]

stock = 'BABA'

# data period
yStart = 2017
mStart = 11
dStart = 14
yEnd = 2018
mEnd = 2
dEnd = 14

import pandas as p
p.core.common.is_list_like = p.api.types.is_list_like
import pandas_datareader.data as pdr

from datetime import datetime
start = datetime(yStart,mStart,dStart)
end = datetime(yEnd,mEnd,dEnd)

f = pdr.DataReader(stock, 'morningstar', start, end)
f.head()

a = 0
b = 0
while a < len(col_data):
    b = 0
    while b < len(f):
        cur = (f.loc[f.index[b], col_data[a]])
        col_data[a].append(cur)
        b += 1
    a += 1

I would like to ultimately be able to print the individual lists ( like print(Open) and retrieve the list of Open prices ). Any advice/additional resources that might help would be appreciated.

Golightly
  • 3
  • 6
  • If you're trying to retrieve a `pandas.DataFrame` column as a list, have you looked into the `.tolist()` method? I might be misunderstanding your question, though. – Tomas Farias Jun 19 '18 at 16:16
  • I have not yet, but will do so immediately. Thank you for the direction – Golightly Jun 19 '18 at 16:38
  • It looks like you are trying to build a list of lists even though you already have a pandas dataframe. Not sure why that would be useful (since a pandas df is essentially a 'fancy' numpy ndarray which is a 'fancy' list of lists) but you could achieve it with something like `col_data = [f['Open'].tolist(), f['High'].tolist(), f['Low'].tolist(), f['Close'].tolist(), f['Volume'].tolist()]` – Leo Jun 19 '18 at 16:59
  • Thank you, Leo. You mention the lack of usefulness of this pursuit: Do you have any suggestions regarding more succinct ways of calculating moving averages/first and second derivatives on individual columns of the dataframe (perhaps, for example, on the Close price data column)? – Golightly Jun 19 '18 at 17:28

0 Answers0