Select an item from multiselect on the sidebar

Hello Guys, please help me on this. I need streamlit to perform an operation when i select an item from multiselect on the sidebar without adding a feature like checkbox or button.
Let’s say the item am selecting is country and i have list of countries to select from, so once i click on that particular country say Algeria, then i get its information that are within my csv file.

3 Likes

Hi Wilson,
I’ve done something similar at work and since I only need a subset of the whole dataframe at any given time I filter my data based on the value of the multiselect menu.

import streamlit as st
import pandas as pd

data = pd.read_csv("example.csv")

# Create a list of possible values and multiselect menu with them in it.
COUNTRIES = data['country'].unique()
COUNTRIES_SELECTED = st.multiselect('Select countries', COUNTRIES)

# Mask to filter dataframe
mask_countries = data['country'].isin(COUNTRIES_SELECTED)

data = data[mask_countries]

Hopefully this works for you!

4 Likes

Thanks a lot Chekos, this helped me greatly. I appriciate your help.

1 Like

Hello @chekos here comes another challenge. I would like to analyze data of particular country based on the selection from the sidebar. Let’s say
selectbox offered me a country Canada. There for, I need to populate data for this country and markdown its explanation just the way OrderedDict works for streamlit hello.py on Demos. Please help

As soon as I select my next item using the multiselect , i face the following error.

File "pandas\_libs\hashtable_class_helper.pxi", line 998, in pandas._libs.hashtable.Int64HashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1005, in pandas._libs.hashtable.Int64HashTable.get_item