Search code examples
pythondataframedictionarykey-valuestreamlit

I am trying to take user inputs and put them into a dataframe via a dictionary, but I get a "Length of values does not match length of index error


I'm currently using the Streamlit library on python to make a webapp for one of my projects that will take user input data from interacting with sliders.

However, when I try to store the user input data into a dataframe via a dictionary, I'm getting the following error:

ValueError: Length of values (4) does not match length of index (1)

I've tried using the pd.Series() method and also working with some of my parameters, but I am stumped.

I feel like I'm missing something from my data structures class here and I need help so I can learn and fix this. Thank you!

Also, this is my first post on stack overflow so I apologize for any bad formatting problems.

This is my code so far:

def user_input_features():

    price_to_earnings = st.sidebar.slider('Price to Earnings', -100, 100, 0)
    debt_to_equity = st.sidebar.slider('Debt to Equity', -50, 50, 0)
    book_value_per_share = st.sidebar.slider('Book Value per Share', -400, 400, 0)
    price_to_book_ratio = st.sidebar.slider('Price to Book Ratio', 0, 500, 0)
    type_of_company = ('Is it a Tech Company?', 0, 1, 0)
    annual_volatility = ('Annual Volatility Swing (In Percent)', 0, 100, 0)

    # Dictionary
    data = {'price_to_earnings': price_to_earnings,
            'debt_to_equity': debt_to_equity,
            'book_value_per_share': book_value_per_share,
            'price_to_book_ratio': price_to_book_ratio,
            'type_of_company': type_of_company,
            'annual_volatility': annual_volatility}

    features = pd.DataFrame(data, index=[0])
    return features

Solution

  • You forgot to add the slider for these two variables (two 4-tuples) :

    type_of_company = st.sidebar.slider('Is it a Tech Company?', 0, 1, 0)
    annual_volatility = st.sidebar.slider('Annual Volatility Swing (In Percent)', 0, 100, 0)
    

    Output :

    enter image description here