Format with function in st.dataframe()

Hi,
I would like to know if it’s possible with the new option column_config if it’s possible to add format which are function ie:
I would like to have for example (from your example):

st.dataframe(
    df,
    column_config={
        "name": "App name",
        "stars": st.column_config.NumberColumn(
            "Github Stars",
            help="Number of stars on GitHub",
            **format= lambda x: x**, # Here to have a function as the format parameter
        ),
        "url": st.column_config.LinkColumn("App URL"),
        "views_history": st.column_config.LineChartColumn(
            "Views (past 30 days)", y_min=0, y_max=5000
        ),
    },
    hide_index=True,
)

The function I would like to use is:

def human_format(num, round_to=1):
    magnitude = 0
    while abs(num) >= 1000:
        magnitude += 1
        num = round(num / 1000.0, round_to)
    return '{:.{}f}{}'.format(num, round_to, ['', 'K', 'M', 'B', 'G'][magnitude])

thx

Afaik the format kwarg in NumberColumn is limited to the “printf-style” formatters shown in the docs. However, the dataframe could be preformatted with your custom function using pandas’ style.format, without it losing the other columns’ customization.

image

st.dataframe(
    df.style.format({"stars": human_format}),   # Preformat
    column_config={
        "name": "App name",
        "stars": st.column_config.NumberColumn(   # Keep as number :)
            "Github Stars",
            help="Number of stars on GitHub",
        ),
        "url": st.column_config.LinkColumn("App URL"),
        "views_history": st.column_config.LineChartColumn(
            "Views (past 30 days)", y_min=0, y_max=5000
        ),
    },
    hide_index=True,
)

Thx it works.

Cordialement

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