I’ve recently colored the rows of my st.dataframe using .style. But all columns with numbers data changed from 6.42, e.g., to 6.42000000. Only columns with string format remained unchanged.
Is there a way to apply .style without changing the original data columns format?
Code used:
prev_data = None
row_styles = []
current_style = None
if len(df6_design['Gama'].unique()) > 1:
# Loop through rows and apply row styles
for index, row in df6_design.iterrows():
current_data = row['Gama']
if current_data != prev_data:
# Change the current style when the product code changes
current_style = 'background-color: #ffffff' if current_style != 'background-color: #ffffff' else 'background-color: #f2f7f2'
row_styles.append(current_style)
prev_data = current_data
else:
# Loop through rows and apply row styles
for index, row in df6_design.iterrows():
current_data = row['Descrição']
if current_data != prev_data:
# Change the current style when the product code changes
current_style = 'background-color: #ffffff' if current_style != 'background-color: #ffffff' else 'background-color: #f2f7f2'
row_styles.append(current_style)
prev_data = current_data
df7_final = df6_design.style.apply(lambda x: row_styles, axis=0)
st.dataframe(df7_final, use_container_width=True, hide_index=True)
You can use the format parameter in st.column_config.NumberColumn to format the columns of st.dataframe. For example, valid formatters include %d %e %f %g %i %u.
Here is the link to the Docs page with further information and example:
Hey @Joao_Couto I had the same issue, posted here a few days ago. Not just that highlighting changes the number format (which I also observed), but that column_config formatter then doesn’t work when the input is df.style.
This forces the column to be in a string format to show the number as 6.42 e not 6.420000000. But, unfortunately, i just need this column to keep its float format, because, if I want to interact with the dataframe, trying to sort this column, for example, it will understand that 9.33 is higher than 81.40, because 9 is higher than 8 in alphabetical sorting.
I will try what dataprofessor said above and see if it works. I’ll let you know if anything changes.
And this worked, the numbers came back to its proper format. BUT, the style thing i did to color the rows disappeared. And i have to say that data_editor style with this bars and pencil in the header is not my thing, i wish i could remain the color style thing and number format using st.dataframe, not data_editor.
The solution i got to use was explained to lawtj in the answers below. But, like I said, it’s not the solution i wanted, because it prohibits me from using the sorting thing of a dataframe.
This happens when i use Round(): TypeError: ‘Styler’ object is not subscriptable
And that’s because Styler does not return a dataframe, but a style object. From there, I started to use apply functions with format(). But, unfortunately it changes to a string format.
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.