Update data in data_editor automatically

Summary

Share a clear and concise description of the issue. Aim for 2-3 sentences.

Steps to reproduce

Code snippet:

import streamlit as st 
import pandas as pd
import numpy as np
df=pd.DataFrame(
    [{"x":1,"y":0},
     {"x":2,"y":0},
     {"x":3,"y":0},
     {"x":4,"y":0},

     ]

    
)

edit_df=st.data_editor(df,num_rows="dynamic")
x1=edit_df.x.dropna()
y1=x1**2
edit_df.y=y1


If applicable, please provide the steps we should take to reproduce the error or specified behavior.

I want to get the input data x col in edit_df, update y col to x*x and show the new y on my app’

Explain what you expect to happen when you run the code above.

Actual behavior: the y col doesn’t update as my expect

Explain the undesired behavior or error you see when you run the code above.
If you’re seeing an error message, share the full contents of the error message here.

Debug info

  • Streamlit version: 1.25.0
  • Python version: 3.9.8
  • PyEnv
  • OS version: windows 10 22h2
  • Browser version: Edge 115.0.1901.203

Additional information

Hey every one, I’m new to streamlit and want to create a simple app which has the function I mention above but finally failed. Could anyone explain why the y col doesn’t update and tell me what happens after I input new number to the dynamic data editor? Will streamlit rerun the script automatically or something else? Thanks a lot!

@BioMastee
Please checkout this code for your solution:

import streamlit as st
import pandas as pd

# Create a DataFrame
df = pd.DataFrame([
    {"x": 1, "y": 0},
    {"x": 2, "y": 0},
    {"x": 3, "y": 0},
    {"x": 4, "y": 0}
])

# Create a dynamic data editor
edit_df = st.dataframe(df, height=400)

# Check if there are changes in the editor
if edit_df is not None:
    # Get the updated 'x' column from the editor
    x_values = edit_df['x']

    # Calculate the corresponding 'y' values
    y_values = x_values ** 2

    # Update the 'y' column in the original DataFrame
    df['y'] = y_values

   

Display the updated DataFrame

st.dataframe(df)

With this code, when you make changes in the dynamic data editor and click outside of the editor, it will update the ‘y’ column based on the updated ‘x’ column and display the updated DataFrame in your Streamlit app.

Thank You

Thank you so much. Since the script raises a mistake that ‘DeltaGenerator’ object is not subscriptable, I guess

edit_df = st.dataframe(df, height=400)

should be

edit_df = st.data_editor(df, height=400)

This partly solve my problem, because the new ‘y’ column is shown in the new dataframe. Coult it be possible to update the new ‘y’ in the first data editor ?
By the way, I guess once I change data in the data editor, the dataframe which is returned by data_editor will only update the data and the app will not change the data shown on the screen. Am I right?
Best regards!