0

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
SystemSigma_
  • 1,059
  • 4
  • 18
  • `type_of_company` and `annual_volatility` are tuples of length four. While `index=[0]` has an index of length one. That doesn't match. – 9769953 May 09 '23 at 10:09
  • (I don't know what the return value/result is of a streamlit slider, but I'm guessing a single value. In which case, I'm not even sure why you want a dataframe for that, since if everything is just a single value, a simple dict suffices.) – 9769953 May 09 '23 at 10:10

1 Answers1

1

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

Timeless
  • 22,580
  • 4
  • 12
  • 30
  • 1
    THANK YOU SO MUCH I can't believe I didn't see this. I've been coding for like 20 hours trying to do this group project on my own and I think I got a bit overloaded. I appreciate the help a lot. THANK YOU!!! I was looking at all the wrong places for the tuples when the answer was probably the silliest thing I did. This is why I need another pair of eyes I guess. Again, I really appreciate it. I can't upvote since i'm new but stackoverflow said they took notice of my vote anyway. – Hyun Jong Lee May 09 '23 at 10:32