How to update the datarame using replace function with session state?

i am building a streamlit project that allow the user to upload dataframe and one of the function is to be able to replace values in columns.

some columns have nan values so i replace it using np.nan for other values i am replacing the old values with the new values.
when the user replace the nan value i used session state to save the new values
than i continue with replacing the other values and save all the new values also using session state.

The problem is that the system either save the new values for nan or the new values for other values and not both of them.

where is the error in my code ?

code:

old_val_repl_list=[]
new_val_repl_list=[]
                
number_of_replacement = st.number_input("Number of values To Replace",0,100) 
if st.button("replace"):    
      for i in enumerate(range(int(number_of_replacement))):
            if i ==(0,0):
                   df_repl=df[columns].replace(np.nan,new_val_repl_list[0],regex=True)
                   if "df_repl" not in st.session_state: 
                           st.session_state.df_repl = df_repl
                        
            if i >(0,0):
                  df_repl=df[columns].replace(old_val_repl_list,new_val_repl_list,regex=True)                    
                  st.session_state.df_repl = df_repl
      st.write(st.session_state.df_repl)
                    
      st.success("{} replace with {} successfully ".format(old_val_repl_list,new_val_repl_list))

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