Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Get data from DataFrame (Please Help!)

Hi Guys, hope you guys are doing great!

It would be much appreciated if someone can guide me how to call the specific data from specific row and column. I have been trying to figure this out for more than 4 hours by reading the errors and google online. Tried multiple ways and still couldn't figure out why.

For example, i'm trying to call whether i'm holding long or short position for [GOOG], i want to call specifically to index[2], columns[0] to get the data saying that i'm holding a 'short' position on [GOOG].
I have tried multiple ways including:
context.stop_loss_df.xs(stock, 'long_short')
context.stop_loss_df.loc[stock, 'long_short']
context.stop_loss_df.get_value(stock, 'long_short')
context.stop_loss_df.get_level_values
and so on... I almost tried any method i could find online and it still doesn't return the string 'short' to me.

log.info(context.stop_loss_df) shows me this, replacing the data in the DataFrame seems fine, but retrieving seems to be a problem. Please help me. This is driving me crazy.

                       long_short  current_price  exec_price  stop_loss  
Equity(42950 [FB])        short       130.905    130.905   133.523  
Equity(47208 [GPRO])       long         12.37      12.37   12.1226  
Equity(46631 [GOOG])      short        783.53     783.53   799.201  
Equity(3212 [GILD])        long         73.75      73.75    72.275  
Equity(8554 [SPY])         long        210.42     210.42   206.212
this is how i label the dataframe

context.stocks = [sid(42950), sid(47208), sid(46631), sid(3212), sid(8554)]  
context.stop_loss_df = pd.DataFrame(index=context.stocks, columns=['long_short','current_price', 'exec_price','stop_loss'])


this is how i replace the data within the dataframe (current_price) was defined  
     for stock in context.stocks:  
                context.stop_loss_df.loc[stock, 'long_short'] = 'long'  
                context.stop_loss_df.loc[stock, 'current_price'] = current_price  
                context.stop_loss_df.loc[stock, 'exec_price'] = current_price  
                context.stop_loss_df.loc[stock, 'stop_loss'] = current_price*0.98


this is how i try to retrieve my data  
        for stock in context.stop_loss_df:  
            if context.stop_loss_df.loc[stock,'long_short'] == 'long':  
                if data.current(stock, 'price') <= context.stop_loss_df.loc[stock, 'stop_loss']:  
                          do_something
3 responses

The dataframe is indexed by the equity object. There are a lot of ways to index and retrieve data from a dataframe but a common way is to use the .loc method. In this case if an equity is passed it will get the row of data corresponding to that equity and return a series. If an equity and a column name is passed it will return just the data in that column for that equity.


goog = sid(46631)  
long_short_result = context.stop_loss_df.loc[goog, 'long_short']

Looks like you may have already tried this but see the attached notebook. It works there. I got the data using a pipeline (which maybe you should look into rather than using data.current unless you need intraday).

There are A LOT of ways to index and retrieve data from a dataframe and this is just one. Perhaps your confusion was that the index is an equity object?

Hope that helps.

Dear Dan, thanks for the reply!

I have figured out that my for loop was looping in a wrong dataframe, df.loc works fine in this case. Thanks for the help! and sorry for the stupid mistake that i made.

instead of:
for stock in context.stocks:
it was:
for stock in context.stop_loss_df:

If I want to get the equity to perform another calculation with the result, how would I proceed to do so?

Like if I want to have just the GOOG Equity but without the data in the other columns, so that I can add it to "data.history(asset, 'price', mom + 1, '1d')" for the position of asset.