Select an item from multiselect on the sidebar

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!

10 Likes