Pandas Dataframe update cell value in SessionState

Hey guys,

quick question. Is there a way preserve a pandas dataframe while using SessionState?
I copyed the file (“”) into my project and load it.

However, if I do an update for one row of the dataframe, the previous values get lost.

state = SessionState.get(df=pd.DataFrame(), index =0)

if st.button("Next"):[state.index, 'Added Values'] = 1
state.index += 1


Any ideas why this is and how to solve it?

Thanks in advance.

Its because your state’s df and index variables are getting reset everytime your script executes,

you could do something like this,
( Assuming you are using Session State class from this gist )

state = SessionState()

if state.df is None:
    state.df = pd.DataFrame()

if state.index is None:
    state.index = 0
1 Like

Sounds pritty easy. The wired thing is, the state.index is not reseted. Only the df.
I took the file from here: .
If i try to do


than it says that 0 parameters were required but one given.
Also state.snyc() does not work. Something is odd here.

Further, when I copy your code from the gist and do
state = _SessionState() I get:
TypeError: init() missing 2 required positional arguments: ‘session’ and ‘hash_funcs’

Any ideas how to solve that?

Hey @chris_klose

Please use the get_state function to get the state.
I like to use a small decorator that I wrote that takes care of get_state and sync state.
Check this out,

Also I like to put the “default configs” in the utils file like this, but it depends how you want to do it,

Hope it helps ! :slight_smile: