I’m trying to python my way through as an easy button, but here’s the situation.
Restaurant names are selected, and the name is used to do a df.loc[‘name’] and pull up the stats for the restaurant. The problem is some restaurants show up more than once in a NYC borough, like “Sweet Chick”, and this will cause an error as pandas can’t pick just one. So I created an index for the data frame, and I want pandas to select the entry by index, but in the selectbox, to keep the user experience friendly, I want to retain the name.
I was curious if you can think of a way to python around this, or are aware of any additional parameters to st.selectbox that i might not be aware of that will accept the name, as well as the index location.
Currently, I have it set up to populate the selectbox with a list of the restaurant names, and also as an experiment, to just use the restaurant name as index to populate the select box.
Github is here: https://github.com/hamiltonchangcodes/yelp_report_cards/tree/master/frontend
I’m thinking about how i might be able to make this work, it could work with some wrangling maybe? Either that or maybe i’m just not comprehending the full utility of format_func
I tried to make an example that comes closer to what you are describing, just run the following code:
import streamlit as st
import pandas as pd
# initialize list of lists
data = [['Le goumet', 10], ['The Alcove', 15], ['Mojo Restaurant', 14], ['Mojo Restaurant', 1]]
# Create the pandas DataFrame
df = pd.DataFrame(data, columns=['Name', 'ID'])
values = df['Name'].tolist()
options = df['ID'].tolist()
dic = dict(zip(options, values))
a = st.sidebar.selectbox('Choose a restaurant', options, format_func=lambda x: dic[x])
st.write(a)
The select box shows names, but when selecting an item, a number is returned. The trick is not create a dictionary with names and respective ids. it also works if 2 names are identical. I hope this is what you were looking for.
I’m trying to put the value into the field which is the same as the value of the previous field on the form, could you please help?
I have tried this but not working. Thank you.