Dependent dropdown lists to filter pandas dataframe and output result

https://discuss.streamlit.io/t/filter-dataframe-by-selections-made-in-select-box/6627/2

Hi,

I only started using Streamlit after discovering it yesterday and have been attempting to use it to create a tool that allows users to select their geographical area from a dataframe (1st dropdown) and based on their selection select a number of general practice surgeries within the selected area (2nd dropdown). I have made a start based on the solution I have linked however I have hit a brick wall at outputing the filtered dataframe as I keep getting a value error when I run the app.

My code thus far is

import streamlit as st
import pandas as pd

st.title(“Place Based Allocation Tool”)

@st.cache
def get_data():
path = “~/Desktop/AIF/ICS Allocation Tool /gp_practice_weighted_population_by_ics v2.xlsx”
return pd.read_excel(path, 1, 0, usecols=“B,F,H,J,L,M,N:AC”)

data = get_data()
data = data.rename(columns={“Region21_7”: “Region”, “STP21_42”: “ICS”, “GP practice name”: “practice_name”})

ics = data[‘ICS’].drop_duplicates()
ics_choice = st.sidebar.selectbox(“Select your ICS:”, ics)
practices = data[“practice_name”].loc[data[“ICS”] == ics_choice]
practice_choice = st.sidebar.multiselect(“Select practices”, practices)

I’d appreciate any help whatsoever to get this working.

Hi @uMehliseli,

First welcome to the Streamlit community!!! :raised_hands: :tada: :star2: :partying_face: :partying_face: :raised_hands: :tada:

Could you send the link to your code for me (GitHub link) or send me the xlsx file? you have hardcoded your path to the xlsx file but I won’t be able to run or test your code snippet (without a bunch of modifications) without one of those two options!

Happy Streamlit-ing!
Marisa

Hi @Marisa_Smith,

Thank you for getting back to me, you can find the file and my code at GitHub - uMehliseli/Allocation_Tool: Prototype for Place Based Allocation Tool. The desired end product for this an app that allows users to selecet an ICS (a regional basically) and then group clinical practices within that ICS and get the aggregated totals of the numerical columns for those groups. Is this something that can be done in Streamlit and are there any similar use cases you could possibly signpost me to if so?

Hey @uMehliseli,

Thanks for the link, I was able to get your dataset. it seems like your code runs fine, even when I put in the Lancashire options with the Darwen healthcare I don’t get the value error you are.

can you take a screenshot of the entire error for me?

I’m pretty sure this is easily done with pandas and the general rule is if you can do it in python then it can be done with Streamlit. I imagine you would just need to sort the data and then get the sum from the sorted/filtered columns.

1 Like