Using Year Range from DF in a Double Slider

Circling back to something I have been toying with for far too long. I am using a double slider, but its only displaying data in a DF from the start and end year of the slider, and for some reason it is not including the range between the years. My data frame has 49 years of data, and I am setting the value to a 5 year span to start with, only I expected to see all data within a range when I update the slider, not just the start and end years.

I am pretty sure its the pandas arg of isin that I need to change.

Thoughts on what i am overlooking?

    select_office = sorted(df['Office'].unique())
    select_office_dropdown = st.sidebar.multiselect('Select one or multiple office(s) to display data:', select_office)
    select_year_range = sorted(df['Year'].unique())
    select_year_slider = st.sidebar.select_slider('Use slider to display year range:', options=select_year_range, value=(2022,2017))
    
    selected_office_year = df[(df.Office.isin(select_office_dropdown)) & (df.Year.isin(select_year_slider))]

    st.map(selected_office_year)
    st.dataframe(selected_office_year.reset_index(drop=True))

Hi @andthisjustin , did you try modifying the latter part of the line ‘selected_office_year
’ to
(df.Year >= year1 & df.Year <= year2), where year1 & year2 will need to be changed to select_year_slider(0) & select_year_slider(1) respectively?

Cheers

Like this @Shawn_Pereira?

selected_office_year = df[(df.Office.isin(select_office_dropdown)) & (df.Year >= select_year_slider(0) & df.Year <= select_year_slider(1))]

When I do that, is says TypeError: ‘tuple’ object is not callable

Hi @andthisjustin , sorry I didn’t have my laptop last time and therefore, couldn’t test the code.

You can reproduce the code and the data at your end to confirm that it is working, before suitably re-purposing it for your use. Change the data file path (i.e. vpth) too as required.

The dataset:
dataset

The code:

import streamlit as st
import pandas as pd
vpth = “D:/ShawnP/Pending/Automation/Python/adhoc tst prgs/”
vfl = “myfile.csv”

myfile = (f"{vpth}{vfl}")

df = pd.read_csv(myfile)

select_office = sorted(df[‘MyOffice’].unique())
select_office_dropdown = st.sidebar.multiselect(‘Select 1/more office(s):’, select_office)
select_year_range = sorted(df[‘MyYear’].unique())
vmax = df[‘MyYear’].max()
vmin = df[‘MyYear’].min()

select_year_slider = st.sidebar.select_slider(‘Year range:’, options=select_year_range, value=(vmax, vmin))
lrng, hrng = list(select_year_slider)[0], list(select_year_slider)[1]
selected_office_year = df.get([‘MyOffice’, ‘MyOtherDataFields’]).where((df.MyOffice.isin(select_office_dropdown)) &
((df.MyYear >= lrng) & (df.MyYear <= hrng))).dropna()
st.dataframe(selected_office_year)

Since, sometimes my copy-paste code blanks some text, I have included the code snapshot too.

Cheers

1 Like

@Shawn_Pereira Thank you very much! You pointed me in the absolute correct direction with that.

Below is ultimately the code I used in my app. Wanted to share if others find it useful.

Can I buy you a coffee/tea for all the help you have provided @Shawn_Pereira ? I know where you are located, got venmo as a thank you? DM if you do.

    select_office = sorted(df['Office'].unique())
    select_office_dropdown = st.sidebar.multiselect('Select one or multiple office(s) to display data:', select_office)
    select_year_range = reversed(sorted(df['Year'].unique()))
    yearmax = df['Year'].max()
    yearmin = df['Year'].min()
    select_year_slider = st.sidebar.select_slider('Use slider to display year range:', options=select_year_range, value=(yearmax, yearmin))
    startyear, endyear = list(select_year_slider)[0], list(select_year_slider)[1]
    
    selected_office_year = df[(df.Office.isin(select_office_dropdown)) & ((df.Year <= startyear) & (df.Year >= endyear))]
    
    st.map(selected_office_year)
    st.dataframe(selected_office_year.reset_index(drop=True))

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