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.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.