如何在 python 中将变量作为另一个函数的参数传递

How to pass a variable as an argument of another function in python

我有一个列表 (chart_list),我想一个一个地调用它们并绘制图表,但我遇到了一个错误。我该如何处理这个问题?我知道它们是字符串,但我不知道如何将其提供给 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}")

问题在于您如何在此循环中访问数据帧:

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])

输出示例:

您不能像这样 (st.line_chart(tickerDf.chart)) 对数据框进行索引,因为这是列名的文字说明。

改用st.line_chart(tickerDf[chart])