St.data_editor: bug after remove lines and add new lines

Hello, I have a local environment with Streamlit 1.41.

Using this example:

import streamlit as st
import pandas as pd

df = pd.DataFrame(
    [
       {"command": "st.selectbox", "rating": 4, "is_widget": True},
       {"command": "st.balloons", "rating": 5, "is_widget": False},
       {"command": "st.dataframe", "rating": 1, "is_widget": False},
       {"command": "st.time_input", "rating": 3, "is_widget": True}
   ]
)
edited_df = st.data_editor(df, num_rows="dynamic")
st.write(len(edited_df))

If I check the checkbox and remove the second row in the data editor, I won’t be able to add new values. They will appear in the data editor, but they won’t show up in print(edited_df), for example.

There’s an unfortunate behavior that comes from pandas automatically creating a new index, which doesn’t show up in the data editor, but is required for a new row to be valid. A simple fix in this case is just to pick one of the actual columns to be the index:

df = pd.DataFrame(
    [
        {"command": "st.selectbox", "rating": 4, "is_widget": True},
        {"command": "st.balloons", "rating": 5, "is_widget": False},
        {"command": "st.dataframe", "rating": 1, "is_widget": False},
        {"command": "st.time_input", "rating": 3, "is_widget": True},
    ]
).set_index("command")

After doing that, it should works fine – you can see it here:

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.