0

I have a python object imported from win2com which contains stock price quotations. numbars is the number of bars in the stock history. quotations is the object that contain the stock quotations.

To retrieve and store the prices and dates, the python code will look something like this;

for x in range(0,num_bars):
    date_quote[x] = quotations(x).Date.date()
    close[x] = quotations(x).Close
    open[x] = quotations(x).Open
    high[x] = quotations(x).Low
    low[x] = quotations(x).High
    volume[x] = quotations(x).Volume
    open_int[x] = quotations(x).OpenInt

I just discovered panda dataframe today and thought it will be better to store the stock quotations data into panda dataframe to make use of the ecosystem built around panda. However, when I looked at the dataframe structure created by pandas_datareader module, it looked highly complicated. Are there any convenient way, like an API or library, to store the stock data from quotations object into panda data frame?

I am using python v3.6

user3848207
  • 3,737
  • 17
  • 59
  • 104

2 Answers2

2

You can do following:

import pandas as pd

df = pd.DataFrame(columns=['Date', 'Close', 'Open', 'Low', 
                           'High', 'Volume', 'OpenInt'])
for i in range(num_bars):
    df.loc[i] = [quotations(i).Date.date(),
                 quotations(i).Close,
                 quotations(i).Open,
                 quotations(i).Low,
                 quotations(i).High,
                 quotations(i).Volume,
                 quotations(i).OpenInt]

After that, you'l have df DataFrame with stock quotations data.

Eduard Ilyasov
  • 3,268
  • 2
  • 20
  • 18
  • May I ask why do you store it in `df.loc`? is this a standard storage location for customized panda data frames? – guagay_wk Oct 11 '17 at 02:07
  • df is a pandas dataframe, or simply 2-dimensial table. By using .loc[i] you can access row of this table by position i. After you acceess it, you can put values to this row of df by assigning list of values with length equal to number of columns of df DataFrame. – Eduard Ilyasov Oct 11 '17 at 02:56
0

Maybe you are looking for:

from pandas_datareader import data aapl = data.DataReader('AAPL', 'google', '1980-01-01')

ChaosPredictor
  • 3,777
  • 1
  • 36
  • 46
  • I am not using dataReader to import from google into panda frame. I would like to import the object `quotations` in my question into panda frame. – user3848207 Oct 09 '17 at 10:43