我的下面的代码允许用户根据行业/子行业/等查找股票回报。
sector='XLF'
subindustry='Insurance Brokers'
tickers = ['']
benchmark = ['^OEX']
stocks = tickers + benchmark + sector + subindustry
这将编译为计算股票收益的df:
stock_return = dataFrame.pct_change()
我想要的是一个plt.plot,它只显示输入数据的行。例如如果有人进入该行业' XLF'和基准 - 我想要那两个绘制。截至目前,如果我这样做,我会收到错误说KeyError:" [' Average' '扇区' '子行业']不在索引"
stocks_sorted = {'T': tickers,
'B': benchmark,
'S': sector,
'Sub': subindustry}
def average_data(stocks):
for stock in stocks:
if len(stocks_sorted['T']) >= 2:
stock_return['Average'] = stock_return[tickers].apply(np.mean,axis=1)
if len(stocks_sorted['Sub']) >=2:
stock_return['Sub-industry'] = stock_return[subindustry].apply(np.mean,axis=1)
if len(stocks_sorted['S']) >= 2:
stock_return['Sector'] = stock_return[sector].apply(np.mean,axis=1)
return stock_return
plt.plot(stock_return[['Average','^OEX','Sector','Sub-industry']]*100)
我的预期输出是仅绘制实际给出数据的列,而忽略其他列(反之亦然)。感谢