I would like to connect the selection options for streamlit multiselect. Lets assume I have the following dataframe Name Color A red A blue B black B blue C green C black C blue D green D yellow D white and two multiselect filters (one for each unique values of each column) I would like the following: When the end user selects an option (or options) for the 'Name' column, the options for the other multiselect for the 'Color' should be updated accordingly and vice versa.
Drop files here
or click to upload
import streamlit as st
import pandas as pd
# Sample DataFrame
data = {
'Name': ['A', 'A', 'B', 'B', 'C', 'C', 'C', 'D', 'D', 'D'],
'Color': ['red', 'blue', 'black', 'blue', 'green', 'black', 'blue', 'green', 'yellow', 'white']
}
df = pd.DataFrame(data)
st.write(df)
# Streamlit App
st.title('Connected Multiselect Filters')
# Select unique names and colors
unique_names = df['Name'].unique()
unique_colors = df['Color'].unique()
# Selected Names
selected_names = st.multiselect('Select Name(s):', unique_names)
# Update Color options based on selected Names
if selected_names:
filtered_colors = df[df['Name'].isin(selected_names)]['Color'].unique()
else:
filtered_colors = unique_colors
# Selected Colors
selected_colors = st.multiselect('Select Color(s):', filtered_colors)
# Update Name options based on selected Colors
if selected_colors:
filtered_names = df[df['Color'].isin(selected_colors)]['Name'].unique()
else:
filtered_names = unique_names
# Display the results
st.write(f'Selected Names: {selected_names}')
st.write(f'Selected Colors: {selected_colors}')
# DataFrame to show current filter status
st.write('Filtered DataFrame:', df[df['Name'].isin(selected_names) & df['Color'].isin(selected_colors)])
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?