How to replace values on a dataframe using pandas and streamlit?

I have a python that allow user to select a column from a given dataframe and change a value cell were he replace the old value with a new value using streamlit.

The problem is that when the user replace the old value with new one the original dataframe is not changing like so :

enter image description here

When I refresh the page or reselect the same column the old value still exist and not the new value.

Where is the error in my code??


import pandas as pd
from pandas.api.types import is_numeric_dtype
import streamlit as st

df = pd.DataFrame({
if st.checkbox("replace"):
columns = st.selectbox("Select  column", df.columns)
old_values = st.multiselect("Current Values",list(df[columns].unique()),list(df[columns].unique()))
with st.form(key='my_form'):
     col1,col2 = st.beta_columns(2)
     st_input = st.number_input if is_numeric_dtype(df[columns]) else st.text_input
     with col1:
          old_val = st_input("old value")
     with col2:
          new_val = st_input("new value")
     if st.form_submit_button("Replace"):

From what I know about streamlit, everytime the page refreshes it executes all code. So it is probably re-creating the df from scratch, and in the process losing your changes.

Try one of 2 things.
Create the data in a function and preface the function with @st.cache
Maybe that will help.
Save the df to a file and then reload that file in instead of creating the df.