Instead of uploading a csv file, i want to create one form which will be converted into dataframe… i want that form to have drop down list for each row. once the user fills the information by using the drop down list, he should be able to add new row… I tried multiple ways but its not working… the closest I got is as below…
If I correctly interpreted your code, the only issue was that setting the index of the selectbox was failing because the row wasn’t populated yet. I made a small tweak to default to 0 for the index, and it seems to work.
import pandas as pd
import streamlit as st
# Define column names for the empty dataframe
columns = ["Person_name", "Location", "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF"]
# Create an empty dataframe with the defined columns
empty_df = pd.DataFrame(columns=columns)
# Define a list of people to choose from
people = ["John", "Mary", "Jane", "Mark", "Lucy"]
# Create an empty session state variable
session_state = st.session_state
# Check if the session state variable is already defined
if "df" not in session_state:
# Assign the initial data to the session state variable
session_state.df = empty_df
session_state.row = pd.Series(index=columns)
# Create a selectbox for each column in the current row
for col in columns:
# Get unique values from the corresponding column in the resource_data dataframe
if col == "Person_name":
values = people
else:
values = ["Y", "N"]
# Create a selectbox for the current column and add the selected value to the current row
index = values.index(session_state.row[col]) if session_state.row[col] in values else 0
session_state.row[col] = st.selectbox(col, values, key=col, index=index)
# Add a button to add a new empty row to the dataframe and clear the values of the selectboxes for the current row
if st.button("Add Row"):
session_state.df = session_state.df.append(session_state.row, ignore_index=True)
session_state.row = pd.Series(index=columns)
# Display the resulting dataframe
st.dataframe(session_state.df)
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.