【问题标题】:connected multiselect filters in streamlit在 streamlit 中连接多选过滤器
【发布时间】:2023-01-11 03:00:51
【问题描述】:

我想连接 streamlit 多选的选择选项。

假设我有以下数据框

Name Color
A red
A blue
B black
B blue
C green
C black
C blue
D green
D yellow
D white

和两个多选过滤器(每列的每个唯一值一个)

我想要以下内容:

  • 当最终用户为“名称”列选择一个选项(或多个选项)时,“颜色”的其他多选选项应相应更新,反之亦然。

到目前为止,这是我不成功的尝试。

import streamlit as st
import pandas as pd

df = pd.DataFrame({

    'Name': ['A', 'A', 'B', 'B', 'C', 'C', 'C', 'D', 'D', 'D', 'D'],
    'Color':['red', 'blue', 'blue', 'black', 'black', 'green', 'blue', 
    'yellow', 'white', 'green', 'purple']
})

tmp = df.copy()

st.title("Connected filters examples")


st.markdown('<br>', unsafe_allow_html=True)

if 'color_class' in st.session_state:
    if st.session_state.color_class:
        tmp = tmp[tmp['Color'].isin(st.session_state.color_class)]
    else:
        tmp = df.copy()

name = st.multiselect(
    'Choose Name',
    tmp['Name'].unique(),
    [], key='name_class')

if 'name_class' in st.session_state:
    if st.session_state.name_class:
        tmp = tmp[tmp['Name'].isin(st.session_state.name_class)]
    else:
        tmp = df.copy()

color = st.multiselect(
    'Choose Color',
    tmp['Color'].unique(),
    [], key='color_class')

st.dataframe(tmp)

例如,如果最终用户选择颜色“红色”和“绿色”,则名称选项应为“A”、“C”和“D”

【问题讨论】:

  • 如果您使用我拥有的代码,您可以通过执行以下操作来解决问题。首先选择颜色蓝色,然后选择名称 A。我希望它只用红色和蓝色更新颜色选择。

标签: python streamlit


【解决方案1】:

这是一个解决方案 - 所有内容都在内联 cmets 中进行了描述

import pandas as pd
import streamlit as st

# Create a dictionary of data
data = {'Name': ['A', 'A', 'B', 'B', 'C', 'C', 'C', 'D', 'D', 'D'],
       'Color': ['red', 'blue', 'black', 'blue', 'green', 'black', 'blue', 'green', 'yellow', 'white']}

# Create a DataFrame from the data
df = pd.DataFrame(data)

# Create an empty DataFrame to store filtered data
df_filtered = pd.DataFrame()

# Initialize the name_select variable
name_select = None

# Get a list of unique color options from the original DataFrame
color_options = df['Color'].unique()

# Create a multi-select widget to select colors
color_select = st.multiselect("Select Color", color_options, key="color")

# Create a flag to track if the color select has been changed
color_select_changed = False

# If the user selects one or more colors:
if color_select:
    # Set the flag to indicate the color select has been changed
    color_select_changed = True
    
    # Filter the original DataFrame to only include rows with the selected colors
    df_filtered = df[df['Color'].isin(color_select)]
    
    # Get a list of unique name options from the filtered DataFrame
    name_options = df_filtered['Name'].unique()
    
    # Create a multi-select widget to select names
    name_select = st.multiselect("Select Name", name_options, key="name")

# If the user selects one or more names, but the color select has not been changed:
if name_select and not color_select_changed:
    # Filter the original DataFrame to only include rows with the selected names
    df_filtered = df[df['Name'].isin(name_select)]
    
    # Get a list of unique color options from the filtered DataFrame
    color_options = df_filtered['Color'].unique()
    
    # Create a multi-select widget to select colors
    color_select = st.multiselect("Select Color", color_options, key="color")

# If the filtered DataFrame is not empty, display it
if not df_filtered.empty:
    st.write(df_filtered)

如果最终用户选择颜色“红色”和“绿色”,名称选项将显示为“A”、“C”和“D”。

希望这可以帮助!

【讨论】:

    猜你喜欢
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    • 2022-10-12
    • 1970-01-01
    相关资源
    最近更新 更多