Storing dictionary in dataframe cell using one-item list

I’ve encountered a difference between how my code runs locally vs. when it’s deployed on Streamlit.

Normally in Python/pandas, a dictionary can be stored in a dataframe cell like this

dict_name = {‘a’:1, ‘b’:2}
df.loc[index, column_name] = [dict_name]

The value for that specific index and column will then be the dictionary. See also the answer to this Stackoverflow question.

However in Streamlit, I’ve noticed that [dict_name] is interpreted and displayed as a dictionary within a list, so as [{‘a’:1, ‘b’:2}] instead of {‘a’:1, ‘b’:2}.

As a result, to grab the value associated with key ‘a’ in the dictionary in the df cell, locally I can run df.loc[index, column_name][‘a’], but in Streamlit, I need to run df.loc[index, column_name][0][‘a’] instead.

Is there anything on my side that I need to change, or is this an inconsistency on Streamlit side?

Could you try st.write(pd.__version__) both locally and when deployed on Streamlit Cloud to see if the pandas versions are the same on both?

I just tried this with pandas 1.4.3 locally and am getting the behavior that you’re experiencing on Streamlit Cloud

import pandas as pd

df = pd.DataFrame({"a": []})

dict_name = {"a":1, "b":2}
df.loc[0, "a"] = [dict_name]

assert df.loc[0, "a"] == [{'a': 1, 'b': 2}] # No error
1 Like