Formatting floating point values as percent in column_config

Summary

How do I transform a floating point value as a percentage in column_config?

lambda x: f'{x:.2f}%'

Code Snipet

df = pd.DataFrame(
    [
       dict(amount=100, percent=0.04),
       dict(amount=120, percent=0.05),
       dict(amount=360, percent=0.03),
   ]
)

edited_df = st.data_editor(
    data=df,
    column_config=dict(
        amount=st.column_config.NumberColumn('Amount', format='$%.2f'),
        percent=st.column_config.NumberColumn('Percent', format='%.2f %%'),

        # TODO: I wish I could do something like this
        # percent=st.column_config.NumberColumn(
        #    label='Percent', 
        #    format_func=lambda x: f'{x*100:.2f}%'
        #),
    )
)

Expected Output

image

Current Output

image

The format in Python would be .2% as in:

> f'{.123:.2%}'
12.30%

However, column_config does not accept that. The format parameter only accepts %d %e %f %g %i %u per the documentation.

As of now, you will have to transform your data to display it (multiply by 100) and deal with it accordingly if you do further computations from it.

Here’s a related request on GitHub you can upvote for custom format functions.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.