OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Remove rows in your dataframe with filling option in streamlit

  • Thread starter Thread starter Quinten
  • Start date Start date
Q

Quinten

Guest
I'm currently creating a streamlit app that shows the results of user's inputs. It may happen that the user accidentally submitted its results. So I want the user to allow removing its values in the dataframe. The most similar option I found was this Deleting rows in st.data_editor progmatically, which uses a callback function to remove the rows. Unfortunately, it only works on the first row of the dataframe but it removes the complete dataset. Also, it doesn't work on the latter rows. Here is some reproducible example:

Code:
import pandas as pd
import streamlit as st

st.sidebar.header("Submit results")

# default values
st.session_state["option"] = ""
st.session_state["number"] = 1

if 'data' not in st.session_state:
    data = pd.DataFrame(columns=["Track", "Result"])
    st.session_state.data = data

data = st.session_state.data

def onAddRow():
    row = pd.DataFrame({'Track':[st.session_state["option"]], 'Result':[st.session_state["number"]]})
    st.session_state.data = pd.concat([st.session_state.data, row])

def callback():
    edited_rows = st.session_state["data_editor"]["edited_rows"]
    rows_to_delete = []

    for idx, value in edited_rows.items():
        if value["x"] is True:
            rows_to_delete.append(idx)

    st.session_state["data"] = (
        st.session_state["data"].drop(rows_to_delete, axis=0).reset_index(drop=True)
    )

with st.form('Form1'):
    st.session_state["option"] = st.selectbox(
            "Select the tracked you played:",
            ("ds Mario Kart", "Toad Harbour", "Koopa Cape"))
    st.session_state["number"] = st.slider("Pick a number", 1, 12)
    st.session_state["submitted1"] = st.form_submit_button('Submit 1', on_click=onAddRow)


columns = st.session_state["data"].columns
column_config = {column: st.column_config.Column(disabled=True) for column in columns}

modified_df = st.session_state["data"].copy()
modified_df["x"] = False
# Make Delete be the first column
modified_df = modified_df[["x"] + modified_df.columns[:-1].tolist()]

st.data_editor(
    modified_df,
    key="data_editor",
    on_change=callback,
    hide_index=True,
    column_config=column_config,
)

Output:

enter image description here

So it looks like this. Imagine we want to remove the second row, we can click on the button in the first column. But this returns an error:

Code:
KeyError: '[1] not found in axis'

I don't understand why this happens. So I was wondering if anyone knows how to create an option to remove rows like above even when you have a filling option with a submit button?
<p>I'm currently creating a <code>streamlit</code> app that shows the results of user's inputs. It may happen that the user accidentally submitted its results. So I want the user to allow removing its values in the dataframe. The most similar option I found was this <a href="https://discuss.streamlit.io/t/deleting-rows-in-st-data-editor-progmatically/46337/2" rel="nofollow noreferrer"><code>Deleting rows in st.data_editor progmatically</code></a>, which uses a callback function to remove the rows. Unfortunately, it only works on the first row of the dataframe but it removes the complete dataset. Also, it doesn't work on the latter rows. Here is some reproducible example:</p>
<pre><code>import pandas as pd
import streamlit as st

st.sidebar.header("Submit results")

# default values
st.session_state["option"] = ""
st.session_state["number"] = 1

if 'data' not in st.session_state:
data = pd.DataFrame(columns=["Track", "Result"])
st.session_state.data = data

data = st.session_state.data

def onAddRow():
row = pd.DataFrame({'Track':[st.session_state["option"]], 'Result':[st.session_state["number"]]})
st.session_state.data = pd.concat([st.session_state.data, row])

def callback():
edited_rows = st.session_state["data_editor"]["edited_rows"]
rows_to_delete = []

for idx, value in edited_rows.items():
if value["x"] is True:
rows_to_delete.append(idx)

st.session_state["data"] = (
st.session_state["data"].drop(rows_to_delete, axis=0).reset_index(drop=True)
)

with st.form('Form1'):
st.session_state["option"] = st.selectbox(
"Select the tracked you played:",
("ds Mario Kart", "Toad Harbour", "Koopa Cape"))
st.session_state["number"] = st.slider("Pick a number", 1, 12)
st.session_state["submitted1"] = st.form_submit_button('Submit 1', on_click=onAddRow)


columns = st.session_state["data"].columns
column_config = {column: st.column_config.Column(disabled=True) for column in columns}

modified_df = st.session_state["data"].copy()
modified_df["x"] = False
# Make Delete be the first column
modified_df = modified_df[["x"] + modified_df.columns[:-1].tolist()]

st.data_editor(
modified_df,
key="data_editor",
on_change=callback,
hide_index=True,
column_config=column_config,
)
</code></pre>
<p>Output:</p>
<p><a href="https://i.sstatic.net/rSNTW6kZ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/rSNTW6kZ.png" alt="enter image description here" /></a></p>
<p>So it looks like this. Imagine we want to remove the second row, we can click on the button in the first column. But this returns an error:</p>
<pre><code>KeyError: '[1] not found in axis'
</code></pre>
<p>I don't understand why this happens. So I was wondering if anyone knows how to create an option to remove rows like above even when you have a filling option with a submit button?</p>
 

Latest posts

Top