Dataframe not using commas for floats that have no decimals

I am trying to take a data frame and display it as a table.

I am using st.dataframe().

The data has a few columns that are floats, but when displaying it will automatically put commas for everything except numbers that have no decimals.

For example:

Hi @Bobby, that is the default behavior of st.dataframe.

In what format would you like the output? Do you want:

  1. Commas for all floats, even those that have no decimals, or
  2. No commas at all

You can customize this behavior using pandas.Styler and pass the pandas.Styler object to st.dataframe. Here’s an illustrative example answering 1. and 2.

import streamlit as st
import pandas as pd

data = {'Month' : ['January', 'February', 'March', 'April'],
        'Expense':[ 21525220.653, 311258408.75, 4099, 56245263.942]}
  
df = pd.DataFrame(data, columns = ['Month', 'Expense'])

st.write("Default: ")
st.dataframe(df)  

st.write("Commas everywhere: ")
s = df.style.format({"Expense": lambda x : '{:,.4f}'.format(x)})
st.dataframe(s)

st.write("No commas: ")
s = df.style.format({"Expense": lambda x : '{:.4f}'.format(x)})
st.dataframe(s)

Happy Streamlit-ing! :balloon:
Snehan

1 Like

Thanks this helps a lot!

1 Like