Using style to color rows impact in numbers format in my st.dataframe

Morning, guys.

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)

Hi @Joao_Couto

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:

Hope this helps!

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.

Thanks for sharing, @lawtj. The solution i got to work was using:

df6_design['Jan-Set (Qtd)'] = df6_design['Jan-Set (Qtd)'].apply(
    '{:,.0f}'.format)

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.

Hello, @dataprofessor. Thanks for your time.

I tried to implement in my last code line this update:

st.dataframe(df7_final, column_config={
        'Jan-Set (Qtd)': st.column_config.NumberColumn(
            'Quantity',
            format='%d'
        )
    }, use_container_width=True, hide_index=True, 
)

The title name changed to ‘Quantity’, but the number format remained 6.420000.

Then i tried to change to:

st.data_editor(df7_final, column_config={
        'Jan-Set (Qtd)': st.column_config.NumberColumn(
            'Quantity',
            format='%d'
        )
    }, use_container_width=True, hide_index=True, 
)

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.

Thanks again for your time.

Hi @Joao_Couto

In that case, could you round the numerical values using Pandas’s built-in function round(), please see this excellent article on how:

Hope this helps!

Hi, @dataprofessor . Thanks for your time.

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.

I also ran into this issue, am finding solution online now.
Have u solved it yet?

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.

Hi, everyone (@aiya6502, @dataprofessor, @lawtj)!

I’ve got the best solution to solve all my issues mentioned above.

This snippet below adjusts the format even after applying ‘.style’.
Check if it’s gonna help you out.

df7_final = df6_design.style.apply(lambda x: row_styles, axis=0)

df7_final = df7_final.format({'% N-1 (Qtd)': "{:,.1f}".format,
                                '% N-1 (€)': "{:,.1f}".format,
                                'Jan-Set (Qtd)': "{:,.0f}".format,
                                'Prev. Out-Dez (Qtd)': "{:,.0f}".format,
                                'Total Ano (Qtd)': "{:,.0f}".format})

st.dataframe(df7_final, use_container_width=True, hide_index=True)
1 Like

This solution work for me, Thank you Joao_Couto

1 Like