How to show thousands separator in a Number Column inside the Data Editor component introduced in streamlit 1.23?
I tried to set the number format to “%,.2f” as it is recognized by the defautl pandas DataFrame Styler, but this format results in erro when used in the Data Editor.
Steps to reproduce
Code snippet:
import pandas as pd
import streamlit as st
data = {
"name": ["Alice", "Bob"],
"value": [50000.5, 40000.4]
}
st.title("Example")
#load data into a DataFrame object:
df = pd.DataFrame(data)
st.data_editor(
df,
column_config={
"value": st.column_config.NumberColumn(format="%,.2f")
}
)
If applicable, please provide the steps we should take to reproduce the error or specified behavior.
Expected behavior:
I expected the Data Editor to be compatible with the same formats as the default DataFrame Styler.
Also it would be great if one could choose which character to use as thousands and decimal separator.
Hi @willhuang, thanks for the quick response, I appreciate it.
Sad to know that implementing this feature is not something simple.
But I still have some doubts about this issue.
I find it curious that when the format parameter is not informed, the number column already shows the values with the thousand separator but without decimal places (try it in my code snippet to check). In this situation, isn’t sprintf.js used?
Wouldn’t it be possible to allow using this standard formatting but with 2 decimal places?
Ah yes you’re right. Huh for some reason, I was trying to check the sprintf format but you can just do something like this:
import pandas as pd
import streamlit as st
data = {
"name": ["Alice", "Bob"],
"value": [50000.5, 40000.4]
}
st.title("Example")
#load data into a DataFrame object:
df = pd.DataFrame(data)
st.data_editor(
df,
column_config={
# NOTICE THE STEP PARAMETER BEING USED HERE
"value": st.column_config.NumberColumn(step=".01")
}
)
Thanks @willhuang, your suggestion achieves the desired behavior and helped me a lot.
I think this solution is sufficient for the moment, but it is not yet ideal, because it implies an effective loss of data accuracy beyond the decimal points defined in the step and not just a visualization with less accuracy.
In my understanding, in an ideal solution the data could have arbitrary amounts of decimal places but only the visualization would show fewer decimal places, so that the data returned would have all decimal places.
Therefore, I think it would be very beneficial to talk to others in the Streamlit team about this issue and perhaps open an Issue to seek a solution to this problem.
Finally, could I use this reply to also ask if there is any way to change the character of the thousand and decimal separator? I’m from Brazil and here we use dot as thousand separator and comma as decimal separator
As far as I know and talking with Lukas Masuch, there is no way to change the character of the thousand and decimal separator as of right now. One solution would be to add a locale and that would be a feature enhancement. I suggest creating a feature enhancement with locale as an option in order to get it prioritized as we do look at github issues and prioritize the ones with high upvotes / interactions.
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)
Hi Julien, thanks for contributing with the thread, but the discussion here is more about the DataEditor than the DataFrame component, which supports pandas styles as you pointed.
Till this day, there is still no way of showing thousands separator in a DataEditor component
Hi, people! I guess everyone is still waiting for an solution.
To help Streamlit prioritize this feature, react with a (thumbs up emoji) to the initial post that can be found here:
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.