How to show results with multi slider

First of all, I am a beginner with python. I come across the following issue, I am able to show results from my xlsx file when I select the Models, but when I add the multi slider I can’t get any results. But if I use st.write I can get the values. I was wondering how I can get the results after selecting the models and price range. Many thank in advance.

import streamlit as st
import pandas as pd
import base64
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import time
import requests
import json

st.title('TWS features')

st.subheader('All data')

@st.cache
def load_data():
    df=pd.read_excel("/Mylocation/TWS Data.xlsx")
    #df=df[df['Price']>0]
    twsdata = df
    return twsdata

twsdata = load_data()

#st.dataframe(df.style.highlight_max(axis=0))

# Sidebar = Model Selection
sorted_unique_model = sorted(twsdata.Model.unique())
selected_model = st.sidebar.multiselect('Model', sorted_unique_model, sorted_unique_model)

# Sidebar = Price Range
min_price = int(twsdata['Price'].min())
max_price = int(twsdata['Price'].max())
#sorted_unique_price = sorted(twsdata.Price.unique())
selected_price_range = st.sidebar.slider('Select the price range', 0, 999, (0, 999), 1)
st.write('Range Values', selected_price_range)

# Filtering data
df_selected_model = twsdata[(twsdata.Model.isin(selected_model)) & (twsdata.Price.isin(selected_price_range))]

st.header('Display selected TWS')
st.dataframe(df_selected_model)

Hi,

I think you are getting 0 results because what you receive from the selected_price_range is a tuple and not a single value.
Therefore, the .isin method you are trying is not what you want.

One possible solution would be to use .between instead of .isin

    df_selected_model = twsdata[(twsdata.Model.isin(selected_model)) & (twsdata.Price.between(selected_price_range[0], selected_price_range[1]))]

Or you could just add the price filters as separate & conditions, however you prefer :slight_smile:

Let me know if this solved your problem.

Best,
Alex

2 Likes

This works. Thank you @Alex_31 for the tip!

1 Like