Search code examples
pythonpandasdataframemergeyahoo-finance

Picking columns to average in pandas (while excluding one)


I'm trying to compare the average of stock returns to a benchmark, and my code so far looks like this:

 index = ['^GSPC']
 tickers = ['CAT','AAPL']
 stocks = tickers + index
 start = dt.datetime(2017,1,1)
 end = dt.datetime(2017,6,30) 

 def excess_movement_plot(stocks):
     f = web.get_data_yahoo(tickers,start,end)
     cleanData = f.loc['Adj Close']
     dataFrame = pd.DataFrame(cleanData)
     stock_return = dataFrame.pct_change() 
     return plt.plot(stock_return[stocks])  

As of now the graph shows three lines, but I'm wanting it to show just 2 -the average of the tickers (AAPL and CAT), and the S&P (^GSPC)

I have tried these two with little success.

 stock_return[['AAPL', 'CAT']].mean(axis=0)
 stock_return.merge('AAPL', 'CAT') 

Solution

  • I think you should be using axis=1, not 0.

    df['Average'] = df[['B', 'C']].mean(axis=1)
    df.head()
    
              A         B         C   Average
    0  0.622956 -1.268788  0.160945 -0.553922
    1  0.934346 -0.218623 -1.431491 -0.825057
    2  0.311257  1.804876  1.791297  1.798086
    3  1.264148  0.811027  0.229493  0.520260
    4 -0.468134 -2.323339  0.151989 -1.085675