How to filter a dataframe using selectbox?

How can I filter a dataframe using a selectbox?

Steps to reproduce

# UNIQUE LISTS FOR MULTISELECT OPTIONS
calc_type_unique = df_all_data['Calc Type'].unique().tolist()
type_unique = df_all_data['Type'].unique().tolist()
priority_unique = df_all_data['Priority'].unique().tolist()
scheme_unique = df_all_data['Scheme'].unique().tolist()

# SIDEBAR
st.sidebar.header("Select options from below:")
calc_type_selection = st.sidebar.selectbox('Calc Type:', calc_type_unique)
type_selection = st.sidebar.multiselect('Type:', type_unique, default=type_unique)
priority_selection = st.sidebar.multiselect('Priority:', priority_unique, default=priority_unique)
scheme_selection = st.sidebar.multiselect('Scheme:', scheme_unique, default=scheme_unique)

# CREATE MASK DEPENDING ON USER INPUT
mask = (df_all_data['Calc Type'].isin(calc_type_selection)) & (df_all_data['Type'].isin(type_selection)) & (df_all_data['Priority'].isin(priority_selection)) & (df_all_data['Scheme'].isin(scheme_selection))

I’m trying to create a mask but the isin for calc_type_selection errors with:

TypeError: only list-like objects are allowed to be passed to isin(), you passed a [str]

Traceback:

File "C:\Users\\Python\Python310\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 565, in _run_script
    exec(code, module.__dict__)File "C:\projects\GIT\2_Backlog.py", line 73, in <module>
    mask = (df_all_data['Calc Type'].isin(calc_type_selection)) & (df_all_data['Type'].isin(type_selection)) & \File "C:\Users\\Python\Python310\lib\site-packages\pandas\core\series.py", line 5563, in isin
    result = algorithms.isin(self._values, values)File "C:\Users\aahmed\Python\Python310\lib\site-packages\pandas\core\algorithms.py", line 459, in isin
    raise TypeError(

Hey @aahmed96515,

Thanks for sharing your question.

The code snippet you shared isn’t runnable as-is (missing import statements and we don’t have access to df_all_data) but it looks like the issue might just be that you’re passing a string to .isin() rather than a list or list-like object.

You could probably just do:
mask = (df_all_data['Calc Type'].isin([calc_type_selection])) & (df_all_data['Type'].isin([type_selection])) & (df_all_data['Priority'].isin([priority_selection])) & (df_all_data['Scheme'].isin([scheme_selection]))

or potentially just use == instead of isin() if it will always be one string. Unfortunately, I can’t confirm that this would definitely resolve the error since the code snippet isn’t runnable.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.