简体   繁体   English

在熊猫中选择平均列(不包括一列)

[英]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) 截至目前,该图显示了三行,但我希望它仅显示2-股票行情的平均价格(AAPL和CAT)以及标普(&GS)

I have tried these two with little success. 我尝试了这两个都没有成功。

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

I think you should be using axis=1 , not 0 . 我认为您应该使用axis=1 ,而不是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM