【问题标题】:Trying to use st.multiselect() in streamlit app not getting desired results尝试在 streamlit 应用程序中使用 st.multiselect() 没有得到想要的结果
【发布时间】:2021-10-27 05:51:15
【问题描述】:

我正在尝试制作一个带有多选功能的数据应用程序来可视化数据。我想为在多选选项中选择的客户端显示一个包含三个参数的分组条形图。但是,无论我选择哪个客户端,图表都以与原始数据相同的顺序显示,即即使我在多选中选择了第 7 个客户端,我仍然会得到数据框中第一行的图表。 代码如下:

data = load_data()

st.markdown('### Client Selection, Offers and Joinings')
clients= data['Client']
clients1=clients.to_list()
options=st.multiselect('Client List',clients1)
st.write(data)

selections=data['selections'] 
offers=data['offers']
joinings=data['joinings']

fig1 = go.Figure()
fig1.add_trace(go.Bar(
    x=options,
    y=selections,
    name='Selections',
    marker_color='indianred'
))
fig1.add_trace(go.Bar(
   x=options,
   y=offers,
   name='Offers',
    marker_color='lightsalmon'
))
fig1.add_trace(go.Bar(
   x=options,
   y=joinings,
   name='joinings',
    marker_color='indianred'
))

# Here we modify the tickangle of the xaxis, resulting in rotated labels.
fig1.update_layout(barmode='group', xaxis_tickangle=-45)
st.plotly_chart(fig1)

有人可以帮我解决这个问题吗? 谢谢

【问题讨论】:

    标签: pandas dataframe numpy plotly streamlit


    【解决方案1】:

    问题出在数据帧的切片上,正确的做法是:

    data1 = load_data()
    
    st.markdown('### Client Selection, Offers and Joinings')
    clients= data1['Client']
    clients1=clients.copy().to_list() 
    options=st.multiselect('Client List',clients1)
    st.write(data1)
    
    if not options:
        filtered_data = data1.copy()
    else:
        filtered_data = data1.loc[data1["Client"].isin(options)]
    
    st.dataframe(filtered_data, width=1000, height=500)
    
    selections=filtered_data['selections'] 
    offers=filtered_data['offers']
    joinings=filtered_data['joinings']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-14
      • 2022-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多