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.
Hello, @aiya6502 . Thanks for commenting. I haven’t solved it. Not the way I wanted. The solution I got was the one I told @lawtj in my answer above.