Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Populating a DataFrame. Am doing manually, but there must be an easier way?

Hi there

I'm tinkering in a notebook and want to print the values of my indicators (upper_band and lower_band) in a table along with current price and date.

The following works and prints 10 days of results, but is my horrible code!

Must be a far cleaner way and also one in which I could dynamically select the amount of day's results (n) to display in table.

Thanks in advance!

:-)

dates = pd.date_range('20150915',periods=10)  
values = [  
          {'close':close[0] ,'lower_band': lower_band[0] ,'upper_band': upper_band[0]},  
          {'close':close[1] ,'lower_band': lower_band[1] ,'upper_band': upper_band[1]},  
          {'close':close[2] ,'lower_band': lower_band[2] ,'upper_band': upper_band[2]},  
          {'close':close[3] ,'lower_band': lower_band[3] ,'upper_band': upper_band[3]},  
          {'close':close[4] ,'lower_band': lower_band[4] ,'upper_band': upper_band[4]},  
          {'close':close[5] ,'lower_band': lower_band[5] ,'upper_band': upper_band[5]},  
          {'close':close[6] ,'lower_band': lower_band[6] ,'upper_band': upper_band[6]},  
          {'close':close[7] ,'lower_band': lower_band[7] ,'upper_band': upper_band[7]},  
          {'close':close[8] ,'lower_band': lower_band[8] ,'upper_band': upper_band[8]},  
          {'close':close[9] ,'lower_band': lower_band[9] ,'upper_band': upper_band[9]},  
          {'close':close[10] ,'lower_band': lower_band[10] ,'upper_band': upper_band[10]}  
           ]

df = pd.DataFrame(values,dates)  
df.head(10)  
6 responses

Where are you getting 'upper_band', 'lower_band', and 'close' from? What type of objects are they. I am assuming they are outputs from a pipeline? If so, then they are pandas series and/or columns in a pandas dataframe. Are you looking for help in how to create a dataframe from series and then display only a portion of the dataframe?

However, if you started with a datafame, a very nice display tool is QGrid. One can filter and sort dynamically. See https://www.quantopian.com/posts/qgrid-now-available-in-research-an-interactive-grid-for-sorting-and-filtering-dataframes. I've attached a notebook showing how to use it.

Maybe I misunderstood?

Tks Dan

I use get_pricing() to produce a time series and then simple math functions to produce the indicators.

I then populate a pd.DataFrame with the prices and indicator lines.

The purpose of this is so i can compare the output to an excel version for "off-by-one" errors.

Tks Ben (I will have a read about Qgrid, thanks for the pointer and attached notebook).

Are you working with a single stock or a number of stocks at once. In other words, do you run 'get_pricing' for just a single stock?

Just a single stock in Notebook at the moment, but as we speak I'm working on adding a universe for the Algo version- Tks

I'd simply add your indicators to the same dataframe which the price data started. Keep it all in one spot. See attached notebook for some ideas. Hope that helps.

Tks Dan! :-)