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 :
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??
code:
import pandas as pd
from pandas.api.types import is_numeric_dtype
import streamlit as st
df = pd.DataFrame({
"source_number":
[11199,11328,11287,32345,12342,1232,13456,123244,13456],
"location":
["loc2","loc1","loc3","loc1","loc2","loc2","loc3","loc2","loc1"],
"category":
["cat1","cat2","cat1","cat3","cat3","cat3","cat2","cat3","cat2"],
})
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"):
df[columns]=df[columns].replace(old_val,new_val)
st.dataframe(df)