# Dataframes in st.data\_editor vs. st.dataframe

**URL:** <https://discuss.streamlit.io/t/dataframes-in-st-data-editor-vs-st-dataframe/45562>\
**Category:** Using Streamlit\
**Created:** [June 19, 2023, 3:03am UTC](https://discuss.streamlit.io/t/dataframes-in-st-data-editor-vs-st-dataframe/45562 "2023-06-19T03:03:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sa28](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/sa28/32/27403_2.png) [@sa28](https://discuss.streamlit.io/u/sa28)\
**Post date:** [June 19, 2023, 3:03am UTC](https://discuss.streamlit.io/t/dataframes-in-st-data-editor-vs-st-dataframe/45562/1 "2023-06-19T03:03:17Z")

</div>

### Summary

Dataframe in `st.session_state` that is being edited in an `st.data_editor` widget is not being updated from other widget actions (in this case, a `file_uploader` widget).

### Steps to reproduce

**Code snippet:**

```auto
import streamlit as st
import pandas as pd

if 'df' not in st.session_state: 
    st.session_state['df'] = pd.DataFrame({'Exclude': [False, False, False]}) 

st.write('Session state dataframe:')
st.dataframe(st.session_state['df'])

file = st.file_uploader('Exclude File')

if file is not None:
    df_load = pd.read_csv(file)
    st.write('Loaded dataframe:')
    st.dataframe(df_load) # not really being used. Actual app uses it.
    st.session_state['df'].loc[0, 'Exclude'] = True
    st.dataframe(st.session_state['df'])

st.write('Session state dataframe after load:')
st.dataframe(st.session_state['df'])

st.write('Session state dataframe in data editor:')
st.data_editor(st.session_state['df'])

```

Using the `file_uploader` widget, a CSV file with the contents below is loaded.

```auto
Exclude
True

```

In the Streamlit app, the file contents are read and used to set the `Exclude` column to `True` in the dataframe’s first row (file is not used here in this example, but it is in the actual app).

After loading the CSV file for the first time, the intended behavior is observed. The dataframe in the `st.data_editor` correctly shows its first row of the `Exclude` column as checked.

Then, if I check rows 2 and 3 of the exclude column in `st.data_editor` (I’m aware that this is not changing the dataframe in `st.session_state`) and reload the CSV file, the first row does not become checked anymore.

**Expected behavior:**

When loading the file, the first row of the dataframe in `st.data_editor` should be checked.

**Actual behavior:**

The first row of the dataframe’s Exclude column should always be checked upon loading the file. The current behavior may well be the intended/expected behavior, but I want to confirm.  
 ![output](https://global.discourse-cdn.com/streamlit/original/3X/0/c/0c6cfbb67b87e1555b6501cd44292a1ae45fa8e1.gif)

### Debug info

- Streamlit version: 1.23.1
- Python version: 3.11.3
- Using Conda? PipEnv? PyEnv? Pex? No
- OS version: macOS Ventura 13.4
- Browser version: Chrome Version 114.0.5735.133 (Official Build) (arm64)

### Additional information

Uploaded a GIF to show the behavior.

---

<div class="post-metadata">

**Author:** ![mathcatsand](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/mathcatsand/32/48006_2.png) [@mathcatsand](https://discuss.streamlit.io/u/mathcatsand)\
**Post date:** [June 26, 2023, 8:38pm UTC](https://discuss.streamlit.io/t/dataframes-in-st-data-editor-vs-st-dataframe/45562/3 "2023-06-26T20:38:17Z")

</div>

Here’s what’s happening:

1. Your script has the initial (all False) dataframe, saved in session state.
2. You load a file and the page reruns.
3. At the beginning of the rerun, your dataframe in session state is not yet updated.
4. When the rerun gets to the file uploader, you execute a line to modify the dataframe saved in session state. It now has the first row as True.
5. You make edits with the data editor, setting the first row back to False and the other rows to True.

At this point, your data editor has an underlying dataframe [T, F, F] with edits setting it to [F, T, T].

1. You remove your file. You code no longer executes the line setting the first row to True, but it doesn’t matter because that change is already committed to session state.
2. You reupload the file and again the code is executed to set the first row to True. However, it is already True from the last time it was edited (directly in session state).
3. The data editor sees no change to the underlying dataframe and so does not reset.

If you did change the underlying dataframe passed to the data editor, all edits it was “remembering” would be discarded, by the way.

---

<div class="post-metadata">

**Author:** ![James\_Martin](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/james_martin/32/30121_2.png) [@James\_Martin](https://discuss.streamlit.io/u/James_Martin)\
**Post date:** [August 1, 2023, 3:54pm UTC](https://discuss.streamlit.io/t/dataframes-in-st-data-editor-vs-st-dataframe/45562/4 "2023-08-01T15:54:30Z")

</div>

> [@sa28](#):
>
> ```auto
> import streamlit as st
> import pandas as pd
> 
> if 'df' not in st.session_state: 
> st.session_state['df'] = pd.DataFrame({'Exclude': [False, False, False]}) 
> 
> if 'edit_df' not in st.session_state:
> st.session_state['edit_df'] = st.session_state['df']
> 
> st.data_editor(st.session_state['edit_df'])
> 
> if st.button("Undo Changes", help="Deletes changes and restores to last commit"):
> st.session_state["edit_df"] = st.session_state["df"]
> st.write("Data Reloaded")
> 
> ```

Hi, I think I have a similar question. I’m not seeing the st.data\_editor getting refreshed. I have a True/False dataframe I want users to interact with (there’s a commit button that’s working to save back to a database) and I’m trying to add an “Undo” button that discards the change. I’ve tried multiple things and I can’t seem to get the editable dataframe to refresh.

Right now, I keep the state of the original dataframe and the editable dataframe in separate session state variables. I thought upon clicking the button, I could make whatever changes I’ve edited reverse on the editable dataframe by reassigning the editable one from the original one.

The behavior I see is that I can click all three Falses, and the checkboxes are now True. When I click undo, I would expect the underlying edit\_df to change and show all False - however, they are all still showing True and have not be refreshed.

Is there a way to force the editable dataframe in data\_editor to show new values based on some process programmatically reprocessing the underlying dataframe?

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/streamlit/original/3X/c/f/cf4c2ee0d938358f82448c5fe4b9c3c91d7ce19c.svg) [@system](https://discuss.streamlit.io/u/system)\
**Post date:** [January 28, 2024, 3:54pm UTC](https://discuss.streamlit.io/t/dataframes-in-st-data-editor-vs-st-dataframe/45562/5 "2024-01-28T15:54:35Z")

</div>

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