Create sliders for a model

Hi I am trying to create sliders for adjusting the values of a model using a dictionary.
The dataframe contains multiple columns of floats.
The index column are unique city names that are used to select a slice of the data.
The column names and index are object type.

def display_sliders():
        sliders = {}
        for col in dti_consolidated_all.columns:
            min_val = dti_consolidated_all[col].min()
            max_val = dti_consolidated_all[col].max()
            val = dti_consolidated_all[col].loc[selected_city]
            sliders[col] = st.slider(col, min_value=min_val, max_value=max_val, value=val)
        return sliders

# Display the slider widgets
sliders = display_sliders()

When I run the code it returns an error:

KeyError: <class 'numpy.float64'>

The error points to the line:

sliders[col] = st.slider(col, min_value=min_val, max_value=max_val, value=val)

Try casting your min_value, max_value, and value inputs to float.

sliders[col] = st.slider(col, min_value=float(min_val), max_value=float(max_val), value=float(val))

(Stylistically, it’s probably better to do it in the lines above where you get the values, but this is one-liner to just illustrate.)

1 Like

Man that did it, can I ask why I needed to cast as floats in the function, when they are already floats?

numpy.float64 is technically a different type that the basic Python float.

1 Like