我希望用户键入某只股票,并让应用程序输出所述股票的图形。我试过了:
if request.method == 'POST':
#text is the stock that the user input
tickers_list = ['aapl', 'ebay', 'nue', 'f', 'tme', 'twtr', 'rblx', 'pfe', 't', 'wfc', 'msft', 'intc', 'tsla', 'pypl', 'hood', 'dis']
text = str(request.form['Tickers'])
print("The text is: ", text, file =sys.stderr)
tickers = yf.Tickers(tickers_list)
df = tickers.download(group_by='tickers')
if df['EBAY'] == text:
print("tickers: ", df.head(5), file =sys.stderr)
df.plot.line()
return render_template("addStock.html")
然而,输出一直给我错误:ValueError:DataFrame的真值不明确。使用a.empty、a.bool((、a.item((、.any((或.all((。
您可以从yfnance获得一个DataFrame,其中包含不同时间点所有股票代码的分组列,例如Open
、High
。
df['EBAY']
的Sub-DF与以下类似:
Open High Low Close Volume Dividends Stock Splits
Date
2022-02-28 54.095833 54.763070 53.189589 54.364719 9280300 0.0 0
2022-03-01 54.334842 54.822818 53.717398 54.125706 9298800 0.0 0
2022-03-02 54.125708 55.221171 53.458475 54.862656 8496700 0.0 0
2022-03-03 55.410386 55.410386 53.239382 54.195419 6139200 0.0 0
2022-03-04 54.065956 56.207082 53.866781 55.430302 8300800 0.0 0
正如错误所示,您必须决定一列/系列中的所有值是应该与字符串text
匹配,还是只匹配一个。您必须对所有列重复此操作,才能得出最终布尔值,该布尔值可用于if
条件。
这是一个精简版:
import yfinance as yf
import pandas as pd
import sys
# sample input
text = 0
df = pd.DataFrame(['foo', 'bar', 'foo'], columns=['EBAY'])
tickers_list = ['aapl', 'ebay', 'nue', 'f', 'tme', 'twtr', 'rblx', 'pfe', 't', 'wfc', 'msft', 'intc', 'tsla', 'pypl', 'hood', 'dis']
tickers = yf.Tickers(tickers_list)
df = tickers.download(group_by='tickers')
# adjust as needed
if (df['EBAY'] == text).any().any():
print("tickers: ", df.head(5), file =sys.stderr)