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)