[英]how to store an image in a variable and pass that variable as a argument in another function?
[英]How to pass a variable as an argument of another function in python
我有一個列表(chart_list),我想一一調用它們,plot 是圖表,但我遇到了錯誤。 我該如何處理這個問題? 我知道它們是字符串,但我不知道如何將其提供給tickerDf。
import streamlit as st
import yfinance as yf
import pandas as pd
import datetime
cols = st.columns(2)
# define the ticker symbol
tickerSymbol = cols[0].text_input("Symbol:", 'GOOG')
st.markdown(f'Shown are the **stock closing** price and **volume** of **{tickerSymbol}**')
# get data on this ticker
tickerData = yf.Ticker(tickerSymbol)
period_list = ['1d', '5d']
selected_period = cols[0].selectbox("Period:", period_list)
interval_list = ['1m', '2m', '5m', '15m', '30m', '60m', '90m', '1h', '1d', '5d', '1wk', '1mo', '3mo']
selected_interval = cols[1].selectbox("Interval", interval_list)
today = datetime.date.today()
yesterday = today + datetime.timedelta(days=-10)
start_date = cols[0].date_input('Start date', yesterday)
end_date = cols[1].date_input('End date', today)
if start_date > end_date:
st.error("Error: End date must fall after start date")
# get the historical prices for this ticker
tickerDf = tickerData.history(interval=selected_interval, start=start_date, end=end_date)
# Open High Low Close Volume Dividends Stock Splits
chart_list = ['Open', 'High', 'Low', 'Close', 'Volume']
selected_charts = st.multiselect("Charts", chart_list)
if st.button("Show"):
for chart in chart_list:
st.line_chart(tickerDf.chart)
st.write(f"## {chart}")
問題是您如何在此循環中訪問 dataframe:
for chart in chart_list:
st.line_chart(tickerDf.chart)
st.write(f"## {chart}")
將其更改為:
for chart in selected_charts:
st.write(f"## {chart}")
st.line_chart(tickerDf[chart])
工作代碼:
import streamlit as st
import yfinance as yf
import pandas as pd
import datetime
cols = st.columns(2)
# define the ticker symbol
tickerSymbol = cols[0].text_input("Symbol:", 'GOOG')
st.markdown(f'Shown are the **stock closing** price and **volume** of **{tickerSymbol}**')
# get data on this ticker
tickerData = yf.Ticker(tickerSymbol)
period_list = ['1d', '5d']
selected_period = cols[0].selectbox("Period:", period_list)
interval_list = ['1m', '2m', '5m', '15m', '30m', '60m', '90m', '1h', '1d', '5d', '1wk', '1mo', '3mo']
selected_interval = cols[1].selectbox("Interval", interval_list)
today = datetime.date.today()
yesterday = today + datetime.timedelta(days=-10)
start_date = cols[0].date_input('Start date', yesterday)
end_date = cols[1].date_input('End date', today)
if start_date > end_date:
st.error("Error: End date must fall after start date")
# get the historical prices for this ticker
tickerDf = tickerData.history(interval=selected_interval, start=start_date, end=end_date)
# Open High Low Close Volume Dividends Stock Splits
chart_list = ['Open', 'High', 'Low', 'Close', 'Volume']
selected_charts = st.multiselect("Charts", chart_list)
if st.button("Show"):
for chart in selected_charts:
st.write(f"## {chart}")
st.line_chart(tickerDf[chart])
Output 示例:
您不能像這樣 ( st.line_chart(tickerDf.chart)
) 對 dataframe 進行索引,因為這是列名的文字說明。
嘗試st.line_chart(tickerDf[chart])
代替
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.