Thousands Separator in a Number Column of Data Editor

EDIT : read too fast thought this was about st.dataframe

Hey guys, I wanted to share the solution I used to properly format numbers when displaying a DataFrame with streamlit.

We can use the style property of a pandas DataFrame, by specifying a formatter (a dict with column names as keys and formatters as values), a thousands character used to separate thousands and a decimal character as decimal separator. This is really useful as, for example, French people use a space as thousands separator and a comma as decimal separator.

Here is an example. I have a df with 4 columns, 3 of which need specific formats :

# df is a pd.DataFrame with columns ["Segment", "Début", "Fin", "Variation"]

# Apply your style to desired columns
df = df.style.format(
    {
        "Début": lambda x : '{:,.1f} €'.format(x),
        "Fin": lambda x : '{:,.1f} €'.format(x),
        "Variation": lambda x : '{:,.1f} %'.format(x),
    },
    thousands=' ',
    decimal=',',
)

# Show the table
st.dataframe(df)

Result :

Hope this helps :slight_smile:

1 Like